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

Ryan McGuire
Ryan McGuire
3,758 Points

Little lost on this one. I get the idea, but I am just not sure on the exact syntax and form so I am just guessin

Rewatched the previous video, but I still am not sure of the exact syntax and form to use. I am surprised there aren't more hints or a referenced document or video. I understand we often need to check online for help, but at this early stage, I am not even sure what I would look for.

racecar.py
class RaceCar:
    color="red"
    def __init__(self, color, fuel_remaining, **kwargs):
        self.color=color
        self.fuel_remaining=fuel_remaining
        for key,value in kwargs.items():
            setattr(self,key,value)

    def run_lap(self, length, laps):
            self.laps=0
            fuel_remaining=fuel_remaining-(length*.125)
            laps+=1

1 Answer

You are asked to add a laps attribute to the class but you added it inside a method, making it a local variable that can only be accessed by the code in that one method. You will want to move it to the top of the class next to your color attribute. The second issue is the fuel_remaining inside the run_lap method. You are once again declaring a local variable instead of accessing the class attribute that was set on the constructor (init). To access that one, you will need to do so via self.attribute in this case, self.fuel_remaining both on the left side and right side of your assignment operator or you can opt for self.fuel_remaining -= length*.125. Note that you will also need to use self.attribute to increment the laps and finally, you should remove the laps from being a parameter in the method definition as it's not being requested on the code challenge and it will fail otherwise because the code challenge validator will not call it passing in that argument.

Ryan McGuire
Ryan McGuire
3,758 Points

Thanks for your help! class RaceCar: color="red" laps=0 def init(self, color, fuel_remaining, **kwargs): self.color=color self.fuel_remaining=fuel_remaining

    for key,value in kwargs.items():
        setattr(self,key,value)
def run_lap(self, length):
    self.fuel_remaining-=length*.125
    self.laps+=1