Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialLuke Tate
Courses Plus Student 2,256 PointsWhy do I get a function call before assignment error?
I have reviewed the video on Methods within the Classes Section 3 times. I have call the use_gas method within the go method (by using a conditional), while I have created the use_Gas method with the appropriate code: a conditional if the gas equal to zero, return False, an increment prier to the conditional, of the gas equaling zero, and the else statement, return True. I have also defined the variable gas, with the int 100 in the init method, above move code. I have checked multiple times, but I keep getting a type error of the "gas" variable not being defined before usage, and the "use_gas" method being called before assignment.
class MuscleCar():
list = ["4 wheels", "big engine", "2 doors", "high-speed", "4 seat"]
"Class Attributes:"
hp = 300
spoiler = False
def __init__(self,make, model, year, top_speed):
#Initializer
self.make = make
self.model = model
self.year = year
self.is_moving = False
self.gas = 100
self.top_speed = top_speed
#Method
#Stop
def stop(self):
print("The car has stopped, but the breaks had to be slammed!")
if self.is_moving:
print("The car has stopped!What a relief!")
self.is_moving = False
else:
("The car has already stopped!")
def go(self, speed):
if self.use_gas():
if not self.is_moving:
print("The car is in motion.")
self.is_moving = True
print(f"The car is going {speed} MPH")
else:
print("Dawg, we already ran out of gas. Now we have to push it to a gas station.")
self.stop()
def use_gas(self):
gas -= 50
if gas <=0:
return False
else:
return True
mCar = MuscleCar('Dodge', 'Charger', 1970, 150)
mCar2 = MuscleCar('Pontiac', 'GTO Judge', 1969, 150)
print(mCar.make+mCar.model)
print(mCar2.make+mCar2.model)
mCar.go(20)
mCar.go(90)
mCar.stop()
print(mCar)
mCar.stop()
mCar2.go(100)
#print(mCar.year)
"""MuscleCar()
mCar = MuscleCar()
print(MuscleCar, mCar)
"Class Attributes:"
print(mCar.list)
print(mCar.list.append("exaust"))
mCar.list.extend([", exaust"])
print(mCar.hp)
print(MuscleCar.hp)
"""
#Instance Attributes:
#mCar.list.extend([", exaust"])
2 Answers
Peter Vann
36,427 PointsHi Luke!
The fix is simple.
Use self.gas (not just gas) in the use_gas method.
That's tripped me up before several times too, so don't feel bad!?!
BTW, for simple testing and debugging I use this (which I just used to run your code and pinpoint the issue):
https://www.katacoda.com/courses/python/playground
I hope that helps.
Stay safe andd happy codiing!
Luke Tate
Courses Plus Student 2,256 PointsSorry to bother any of you who would replied, but the problem was solved over a simple mistake, I easily over looked. Any response will be highly appreciated. ..
Peter Vann
36,427 PointsI do a lot of debugging on w3schools, jsfiddle and Codepen, too.
For SQL:
For RegEx:
I hope that helps.
Stay Safe and happy coding!
Luke Tate
Courses Plus Student 2,256 PointsLuke Tate
Courses Plus Student 2,256 PointsI will check this out. Thanks for your great guidance. It's nice to know that there are multiple coding debuggers that are free to use, and easily accessible.