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 trialDezhi Zhu
2,662 PointsJesus 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?
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
39,021 PointsHi Dezhi,
There are two issues with your code:
- 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 removelaps
as an argument in your__init__
method. - There is no
self.length
. Just use the passed inlength
variable.
Let me know if you're still confused - if you solve it, post your code here!
Cheers
-Greg
Flore W
4,744 PointsHey 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
39,021 PointsGood 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
4,744 PointsThanks Greg, all clear now!
Oszkár Fehér
Treehouse Project ReviewerOszkár Fehér
Treehouse Project ReviewerI don't think 'he' can answer to this question but some coders may do :))