2021年4月25日星期日

attribute not defined error even though it is defined on a global scope

I'm a newbie to python so please bear with me. I was making a class that contains the make, model, and fuel of a car and was experimenting with the set and get methods to try and learn their use. The issue I'm running into is when I try to update my tank attribute by running my setFuel method and print out the updated fuel integer I get error tank is not defined even though I defined it all the way above. Why is this occurring and how can I fix it. I added comments all over my code to explain what I tried to do to help you understand my code. Any help is appreciated thanks in advance.

class Car:      tank = 0 #to keep track of fuel      make = ""      model = "" #to keep track of model            def __init__(self, make, model, fuel):          self.make = make          self.model = model          self.fuel = fuel                  #returns the amout of fuel currently in the tank(as an integer)         def getFuel(self):          return int(self.fuel)        #sets the amounts of fuel currently in the tank(as an integer)      def setFuel(self, tank):          self.tank = int(tank)        #returns the make and model of the car as a string      def getType(self):          return str(self.make),(self.model)        def printFuel(self):          print(self.fuel)    #Instantiate a new Car in a variable  myCar = Car("Ford", "Escort", 10)  yourCar = Car("Ford", "Escape", 14)    #print statement that prints the result of the "getFuel"  #method of the "myCar" object  myCar.printFuel()  yourCar.printFuel()    #Change the "tank" attribute to modify the fuel in the tank using  #setFuel method of the "myCar" and "yourCar" object  #subtract 7 from from the tank  myCar.setFuel(tank-7)  #subtract 5 from the tank   yourCar.setFuel(tank-5)    #print statement that prints the result of the "getFuel"  #method of the "myCar" and "yourCar" object  print(myCar.getFuel())  print(yourCar.getFuel())  print(myCar.getType())  print(yourCar.getType())  
https://stackoverflow.com/questions/67259769/attribute-not-defined-error-even-though-it-is-defined-on-a-global-scope April 26, 2021 at 09:24AM

没有评论:

发表评论