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 trialhector alvarado
15,796 PointsIDK what's wrong with my class
the instruction of the code it's:
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 attr, value in kwargs.items():
setattr(self, attr,value)
def run_lap(self, length):
fuel_remaining = fuel_remaining - (length * 0.125)
laps =+ 1
4 Answers
Oszkár Fehér
Treehouse Project ReviewerHi Hector Your class looks okey. the methods are built well it's just one 'thing' actually self it's missing and a little typo
class RaceCar:
laps = 0
def __init__(self, color, fuel_remaining, **kwargs):
self.color = color
self.fuel_remaining = fuel_remaining
for attr, value in kwargs.items():
setattr(self, attr,value)
def run_lap(self, length):
fuel_remaining = fuel_remaining - (length * 0.125) <--- from this line
laps =+ 1 <--- and from this line also the =+ it should be +=
When you use class attributes or functions it has to be called with the 'self' instance
class RaceCar:
laps = 0
def __init__(self, color, fuel_remaining, **kwargs):
self.color = color
self.fuel_remaining = fuel_remaining
for attr, value in kwargs.items():
setattr(self, attr,value)
def run_lap(self, length):
self.fuel_remaining = self.fuel_remaining - (length * 0.125)
self.laps += 1
I hope it helps. Happy coding
Oszkár Fehér
Treehouse Project ReviewerNo worries, it happens with all of us. We learn by doing mistakes. Keep up the good work.
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
micram1001
33,833 PointsIf I understand this problem, Laps is set to zero before the methods. Think about scope. Length is not made an instance since it’s used in the method run_lap only.
hector alvarado
15,796 Pointshector alvarado
15,796 PointsOMG thanks, damit... The devil is in the detail right?