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

How to increment a value when a certain method is called?

I am not 100% sure that my run_lap method is properly written. Also I am unsure how to increment the 'laps' value for every time the run_lap method is called.

Incidentally, does anyone know if only certain challenges have 'hints'? I liked how some classes gave hints to help problem solve. Thanks!

racecar.py
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):
        self.length = length
        self.fuel_remaining = self.fuel_remaining() - (length * .125)
        return self.length

        for run_lap():
            self.laps = self.laps +=1

1 Answer

Your run_lap method will only run the code up until the return value, at that point it exists the function and returns the value of self.length to the caller. Anything after that is not run; This bit of code: self.length = length is not necessary as the length of the laps is only relevant to that method, so you don't need to make it a class attribute; self.fuel_remaining(), this looks like it was an accident, you are calling a method called fuel_remaining but you probably want to access the class attribute instead (you should remove the parenthesis); for run_lap(): this is not correct syntax and even if it was, it would not run as per the return written above; self.laps = self.laps +=1 this assignment is a little bit confusing, usually to add 1 to something you do it one of two ways: foo = foo + 1 or foo += 1 but you sort of mixed them both;