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 trialnishitbodawala
5,924 Pointscan any one explain how to solve this?
Vrroom! OK, now let's add a method named run_lap. It'll take a length argument. It should reduce the fuel_remaining attribute by length multiplied by 0.125. Oh, and add a laps attribute to the class, set to 0, and increment it each time the run_lap method is called.
class RaceCar:
laps=0
def __init__(self,color,fuel_remaining,**kwargs):
self.color=color
self.fuel_remaining=fuel_remaining
for key,value in kwargs.items():
setattr(self,key,value)
def run_lap(self,length):
use_fuel = length * 0.125
self.fuel_remaing-= use_fuel
laps += 1
4 Answers
asad modin
165 Pointsclass RaceCar(): laps = 0 def init(self,color,fuel_remaining,**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
Asma Al-Marrikhi
45,525 Points def run_lap(self, lenght):
self.fuel_remaining -= lenght * 0.125
self.laps += 1
You forget to add self before laps attribute in the run_lap method
Kyle Salisbury
Full Stack JavaScript Techdegree Student 16,363 Pointswont matter too much since length is spelled the same through out your code but spelling length correctly might affect some other peoples code.
jack jack
Python Web Development Techdegree Student 208 Pointsclass 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
Chad Goldsworthy
4,209 PointsJust for anyone still struggling with this, I think I managed to find the issue. I tried all the code other people posted and said worked, but it did not work for me (and it seems there were many people having the same issue).
I ran it through my IDE and realised the length*0.125 in the run_lap method was causing a TypeError: unsupported operand type(s) for -=: 'str' and 'float'
So, when assigning fuel_remaining in the init method, you should convert it to a float as well, like: self.fuel_remaining = float(fuel_remaining)
I say "you should" lightly though, I'm no expert but it's just what fixed the issue for me.
This is the code that eventually worked for me:
class RaceCar:
laps = 0
def __init__(self, color, fuel_remaining, **kwargs):
self.color = color
self.fuel_remaining = float(fuel_remaining) # <-- this is what fixed it for me,
# adding the float() method
for key, value in kwargs.items():
setattr(self, key, value)
def run_lap(self, length):
self.fuel_remaining -= (length * 0.125)
self.laps += 1
Jon Hockley
3,781 PointsJon Hockley
3,781 PointsRather than tell you the correct answer:
Good luck!