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

Dezhi Zhu
Dezhi Zhu
2,662 Points

Jesus what's wrong here?

I check several times. It seems my code just like the right code it requires. BUT why it doesn't work?

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,values in kwargs.items():
            setattr(self,key,values)

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

1 Answer

Greg Kaleka
Greg Kaleka
39,021 Points

Hi Dezhi,

There are two issues with your code:

  1. You shouldn't the laps attribute as a parameter, because it should ALWAYS be zero when initializing a new race car. If you take it as a parameter in the initializer, someone may try to create a race car that already has 5 laps. That doesn't make sense, but then you're also ignoring that input. The solution is to simply remove laps as an argument in your __init__ method.
  2. There is no self.length. Just use the passed in length variable.

Let me know if you're still confused - if you solve it, post your code here!

Cheers :beers:

-Greg

Flore W
Flore W
4,744 Points

Greg Kaleka

Hey Greg, I had the same issue (putting laps inside the initialiser method), and I don't quite get your answer since laps is set to 0 inside the init function (self.laps = 0), so even if I define mycar = RaceCar("red", 10, 5), I would still get mycar.laps = 0, and not 5. Why wouldn't it work then and what's the difference between setting laps to 0 outside of the init method and inside the init method? [I agree on the second point so assuming the second point is solved.]

Greg Kaleka
Greg Kaleka
39,021 Points

Flore Wang,

Good point. I've edited my answer above. You still shouldn't take laps as an init argument, but you should set laps = 0 inside the init method, not outside of it.

Flore W
Flore W
4,744 Points

Thanks Greg, all clear now!