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

OOP - method and variable Declaration issue - Assistance Required.

Hi,

I am not understanding my error, and what I am doing wrong in this exercise.

I have been requested to make sure that the 'laps' attribute is referenced in the init method. I therefore removed the 'laps' attribute for the RaceCar class.

I am getting Error message that do not explain where I am going wrong. Kindly assist.

racecar.py
class RaceCar:

    def __init__(self, color, fuel_remaining, laps, **kwargs):
        self.color = color
        self.fuel_remaining = fuel_remaining
        self.laps = laps

        for key, value in kwargs.items():
            setattr(self, key, value)

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

1 Answer

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there, Emmanuel Ade! You're doing fantastic and you're right at the end of this task. The problem here, is that they want laps to be set to 0 by default. This means that when creating a new RaceCar instance, they must send in the color and the fuel_remaining, but they shouldn't necessarily have to specify the laps. They could, but it wouldn't be required. We can do this by assigning laps a default value in the __init__.

def __init__(self, color, fuel_remaining, laps=0, **kwargs):

That tells Python that if we don't send in anything for laps, to go ahead and assign a value of 0 to laps. Which makes sense, in my opinion. If we just built a brand new race car and it's coming off the assembly line, then it's never run any laps before :smiley:

Hope this helps! :sparkles: