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 trialHARPREET SINGH
146 PointsHow to initialize laps to 0
Ho wto initialise laps to 0
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
231,248 PointsThe 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
146 PointsHARPREET SINGH
146 Pointsany 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
231,248 PointsSteven Parker
231,248 PointsThe "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.