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

Thomas Beaudry
Thomas Beaudry
29,084 Points

Python Code Challenge 2 of 3 in Object -Oriented

Hey guys, have been at this for some time and cannot seem to see my problem. Thanks for helping, much appreciated.

Challenge Task 3 of 3

Great! One last thing. In Python, attributes defined on the class, but not an instance, are universal. So if you change the value of the attribute, any instance that doesn't have it set explicitly will have its value changed, too! For example, right now, if we made a RaceCar instance named red_car, then did RaceCar.laps = 10, red_car.laps would be 10! 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!

racecar.py
class RaceCar:
    laps=0
    def __init__(self, color, fuel_remaining, laps=0, **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 * 0.125)
        self.laps += 1

3 Answers

Evan Trimby
Evan Trimby
5,381 Points

laps does not need to be an argument for the method, and at this point you are passing the argument but not doing anything with it. Just set it right below the line for fuel_remaining.

Zach Savage
seal-mask
.a{fill-rule:evenodd;}techdegree
Zach Savage
Front End Web Development Techdegree Student 11,784 Points
class RaceCar:
    def __init__(self, color, fuel_remaining, **kwargs):
        self.color = color
        self.fuel_remaining = fuel_remaining
        self.laps = laps = 0
        for x, y in kwargs.items():
            setattr(self, x, y)

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

this worked for me

This one worked for me

Class RaceCar(): def init(self, color, fuel_remaining, **kwargs): self.color = color self.fuel_remaining = fuel_remaining self.laps = 0

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

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