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 trial

Python Object-Oriented Python Instant Objects Master Class

RaceCar 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.

racecar.py
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

Also I changed fuel_remaining in run_lap to self.fuel_remaining

1 Answer

Steven Parker
Steven Parker
231,269 Points

Good call changing "fuel_remaining" to "self.β€Œfuel_remaining". :+1:

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

Ok thank you that fixed the problem. One other question why not add self.laps to my init method?

Steven Parker
Steven Parker
231,269 Points

That's actually a good idea, and task 3 of the challenge will ask you to do exactly that!