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

HARPREET SINGH
HARPREET SINGH
146 Points

How to initialize laps to 0

Ho wto initialise laps to 0

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.fuel_remaining -=  0.125*self.length
        self.lap += 1

1 Answer

Steven Parker
Steven Parker
230,995 Points

The initialization is OK for task 2, but:

  • "length" is a parameter, so it should not have the "self." prefix added to it
  • the last line of the method is incrementing "lap" instead of "laps" (with an "s')
HARPREET SINGH
HARPREET SINGH
146 Points

any write up where I can get more clarity on "why length, which is parameter here does not require "self" prefix". I still need to get more understanding on same.

Steven Parker
Steven Parker
230,995 Points

The "self" prefix represents the object itself and is used to refer to instance variables that are stored inside the object, such as "laps".

But "length" is the name of a parameter that is passed to the method, and isn't stored (it will go away when the method ends). So you reference it by name without any prefix.