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 What is wrong with this code?

I am always getting a Bummer: Try again

racecar.py
class RaceCar:

    def __init__(self, color, fuel_remaining, laps, **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

2 Answers

Kevin Brennan
Kevin Brennan
19,920 Points

Hi Andrew,

You are very close. The problem you have is that you have not assigned self.laps to laps in your init. When you are asked to set the laps to the value of 0 you do that when you first declare laps.

class RaceCar:
    def __init__(self, color, fuel_remaining, laps=0, **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

I hope that is clear and is of help.

Kevin Brennan

Kevin Brennan
Kevin Brennan
19,920 Points

Hi Philip, That is a very good question, the answer is actually quite complicated and is quite a point for discussion. Below is a link to stackoverflow where this is discussed:

https://stackoverflow.com/questions/14037768/what-is-the-difference-writing-code-in-a-class-and-in-def-init-self-in-pyth

I hope that makes it a bit clearer.

Regards Kevin