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

Hi, It does not like my solution, but it works just fine on my end. It is very hard to understand without error message

class RaceCar:

    color = 'blue'
    fuel_remaining = 10
    laps = 0

    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 = self.fuel_remaining - length * 0.125
        self.laps += 1
racecar.py
class RaceCar:

    color = 'blue'
    fuel_remaining = 10
    laps = 0

    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 = self.fuel_remaining - length * 0.125
        self.laps += 1

4 Answers

Steven Parker
Steven Parker
229,786 Points

The instructions say "add a laps attribute to the class, set to 0" but your init now requires laps to be provided to create the instance. The challenge isn't expecting the creation of an instance to have changed from task 1.

When the challenge tests your code, it creates an instance using the parameters that the instructions asked you to use. Requiring any additional parameters causes the creation (and therefore the test) to fail.

Also, the class is intended to have the laps value always start at 0 for a newly-created instance, so allowing (even if not requiring) it to be passed as a construction parameter changes the intent of the class design.

Thank you Steven, The third part of the challenge is asking the following " To prevent this, be sure to set the laps attribute inside of your init method (it doesn't have to be a keyword argument, though). If you already did it, just hit that "run" button and you're good to go!" ; Therefore, I added the laps attribute to the init function.

So, I needed to set the laps attribute to zero in the init function without accepting the laps value as an argument in the init function. I don't see why we need to do that. We could pass the laps attribute to init function while creating the instance and this will prevent the value of the laps from being overwritten if the value of laps attribute is changed in the class. Please let me know if my assumption is correct?

Steven Parker
Steven Parker
229,786 Points

I updated my answer to explain this in more detail.

Thank you Steven!