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 trialYu-Che Hung
Courses Plus Student 10,607 Pointsstuck in def run_lap method
stuck in def run_lap method
class RaceCar:
def __init__(self, color, fuel_remaining, **kwargs):
self.color = color
self.fuel_remaining = fuel_remaining
for k , v in kwargs.items():
setattr(self, k, v)
def run_lap(self, length):
self.fuel_remaining = self.fuel_remaining - (length * 0.125)
self.laps=0
6 Answers
Steven Parker
231,269 PointsRegarding the laps
attribute, the instructions say to "increment it each time the run_lap
method is called", but the method currently resets it to 0 on each call.
But also remember to initialize it to 0 in the constructor.
Yu-Che Hung
Courses Plus Student 10,607 PointsI tried but it didn't pass the challenge.
Yu-Che Hung
Courses Plus Student 10,607 Pointsclass RaceCar: def init(self, color, fuel_remaining, **kwargs): self.color = color self.fuel_remaining = fuel_remaining for k , v in kwargs.items(): setattr(self, k, v)
def run_lap(self, length,laps=0):
self.fuel_remaining = self.fuel_remaining - (length * 0.125)
self.laps += 1
Steven Parker
231,269 PointsIt doesn't look like you remembered to initialize it to 0 in the constructor.
Yu-Che Hung
Courses Plus Student 10,607 Pointsclass RaceCar: def init(self, color, laps=0, fuel_remaining, **kwargs): self.color = color self.laps = laps self.fuel_remaining = fuel_remaining for k , v in kwargs.items(): setattr(self, k, v)
def run_lap(self, length):
self.fuel_remaining = self.fuel_remaining - (length * 0.125)
self.laps += 1
Yu-Che Hung
Courses Plus Student 10,607 PointsYou mean this?
Yu-Che Hung
Courses Plus Student 10,607 Pointsclass RaceCar: def init(self, color, fuel_remaining, **kwargs): self.color = color self.fuel_remaining = fuel_remaining for k , v in kwargs.items(): setattr(self, k, v)
laps = 0
def run_lap(self, length):
self.fuel_remaining = self.fuel_remaining - (length * 0.125)
self.laps +=1
Yu-Che Hung
Courses Plus Student 10,607 PointsYu-Che Hung
Courses Plus Student 10,607 Pointsthank you steven but how to "increment it each time the run_lap method is called"?
Steven Parker
231,269 PointsSteven Parker
231,269 PointsThe most common way would be:
self.laps += 1