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

Doruk Özdemir
Doruk Özdemir
10,736 Points

True code is not working

This code is actually true and does what instructions say, I tried it in my own shell. Yet, what's the problem here that I cannot see ?

It gives an error that "Be sure to set 'laps' to 0" , but it is set to 0.

racecar.py
class RaceCar:

    def __init__(self,color,fuel_remaining,**kwargs):

        self.color = color
        self.fuel_remaining = fuel_remaining

        for attr,value in kwargs.items():

            setattr(self,attr,value)


    global laps
    laps = 0

    def run_lap(self,length):
        global laps
        self.length = length

        self.fuel_remaining -= self.length*0.125

        laps += 1

1 Answer

laps should be inside the init, if not it is 'outside' the class it doesnt belong to it, its outside as a global variable that can be used by the class but it doesnt belong to it. Its better to avoid the usage of global.

     def __init__(self, color, fuel_remaining, **kwards): 
         self.laps = 0
         self.color = color
         self.fuel_remaining = fuel_remaining
         for attr, value in kwargs.items():
             setattr(self, attr, value)

and later inside your run_lap method use the previously defined laps as such

    def run_lap(self, length):
        # I dont understand why yo define length to be a property inside the class
        # self.lengt=length
        self.fuel_remaining -= length*0.125
        self.laps += 1