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

Yu-Che Hung
PLUS
Yu-Che Hung
Courses Plus Student 10,607 Points

stuck in def run_lap method

stuck in def run_lap method

racecar.py
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
Steven Parker
231,269 Points

Regarding 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
Yu-Che Hung
Courses Plus Student 10,607 Points

thank you steven but how to "increment it each time the run_lap method is called"?

Steven Parker
Steven Parker
231,269 Points

The most common way would be:

        self.laps += 1
Yu-Che Hung
PLUS
Yu-Che Hung
Courses Plus Student 10,607 Points

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,laps=0):
    self.fuel_remaining = self.fuel_remaining - (length * 0.125)
    self.laps += 1
Steven Parker
Steven Parker
231,269 Points

It doesn't look like you remembered to initialize it to 0 in the constructor.

Yu-Che Hung
PLUS
Yu-Che Hung
Courses Plus Student 10,607 Points

class 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
PLUS
Yu-Che Hung
Courses Plus Student 10,607 Points

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)

laps = 0   


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