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 trialsage georgi
2,005 PointsRaceCar class
I have no idea why this wont work and I get an error in visual studio "using variable 'laps' before assignment" Any help would be greatly appreciated.
class RaceCar:
laps = 0
def __init__(self,color, fuel_remaining,**kwargs):
self.color = color
self.fuel_remaining = fuel_remaining
for color, fuel_remaining in kwargs.items():
setattr(self,color,fuel_remaining)
def run_lap(self,length):
self.fuel_remaining = fuel_remaining - length * 0.125
laps = laps + 1
1 Answer
Steven Parker
231,269 PointsGood call changing "fuel_remaining" to "self.βfuel_remaining".
Now just do the same to both references to "laps" on the last line and you should pass.
Also, you can make these kinds of things less likely and compact your code a bit by using the "addition (or subtraction) assignment" operators:
self.fuel_remaining -= length * 0.125
sage georgi
2,005 PointsOk thank you that fixed the problem. One other question why not add self.laps to my init method?
Steven Parker
231,269 PointsThat's actually a good idea, and task 3 of the challenge will ask you to do exactly that!
sage georgi
2,005 Pointssage georgi
2,005 PointsAlso I changed fuel_remaining in run_lap to self.fuel_remaining