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

racecar.py - need help

In Python, attributes defined on the class, but not an instance, are universal. So if you change the value of the attribute, any instance that doesn't have it set explicitly will have its value changed, too!

For example, right now, if we made a RaceCar instance named red_car, then did RaceCar.laps = 10, red_car.laps would be 10!

To prevent this, be sure to set the laps attribute inside of your init method (it doesn't have to be a keyword argument, though). If you already did it, just hit that "run" button and you're good to go!

racecar.py
class RaceCar:
    laps = 0
    def __init__(self, color, fuel_remaining, laps, **kwargs):
        self.color = color
        self.fuel_remaining = fuel_remaining
        self.laps = laps
        for key, value in kwargs.items():
            setattr(self, key, value)

        self.laps = laps

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

9 Answers

Omid Ashtari
Omid Ashtari
4,813 Points

stuck in the same place, help please :)

class RaceCar:

    def __init__(self,color,fuel_remaining, **kwargs)    
        self.color = color
        self.fuel_remaining = fuel_remaining
        self.laps = 0

        for (key,value) in kwargs.items():
            setattr(self,key,value)

    def run_lap(self,length):
        self.fuel_remaining -= length*0.125
        self.laps += 1
Christian Mangeng
Christian Mangeng
15,970 Points

The colon after the closing parenthesis in the init method is missing.

Omid Ashtari
Omid Ashtari
4,813 Points

ARGH :( ...thank you very much

Christian Mangeng
Christian Mangeng
15,970 Points

Hi Jay,

your run_lap method is missing the length parameter:

def run_lap(self, length):

Also, you dont't need a length attribute in the RaceCar's init method. It should be laps.

Christian Mangeng
Christian Mangeng
15,970 Points

Hi Pankaj,

two things here:

1) You now need to add the laps attribute and its value assigned to the init method of the class. This is to avoid defining a universal value for laps.

def __init__(self, color, fuel_remaining, laps=0, **kwargs)
    self.laps = laps
    self.color = color
    self.fuel_remaining = fuel_remaining

2) You have "self.laps = laps" two times in the init method, so remove the one below the for loop

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

(you don't need laps in the __init__'s arguments :D)

Christian Mangeng
Christian Mangeng
15,970 Points

Hi Kenneth,

thank you for the comment. I see, so it's better to do it like that?

def __init__(self, color, fuel_remaining, **kwargs)
    self.laps = 0
    self.color = color
    self.fuel_remaining = fuel_remaining
Jay Norris
Jay Norris
14,824 Points

Here's my best guess, but it's still just saying "Bummer, Try Again!!":

class RaceCar:
    def __init__(self, color, fuel_remaining, laps=0, **kwargs):
        self.color = color
        self.fuel_remaining = fuel_remaining
        self.length = length
        for key, value in kwargs.items():
            setattr(self, key, value)

    def run_lap(self):
        self.fuel_remaining -= length * 0.125
        self.laps += 1
Chris Christensen
Chris Christensen
9,281 Points

Hi Jay,

Not sure if you figured it out yet but the only thing you are missing is the attributing setting for laps.

def __init__(self, color, fuel_remaining, laps=0, **kwargs):
    self.color = color
    self.fuel_remaining = fuel_remaining
    self.length = length
    self.laps = laps

Happy coding!

Chris

Herman Brummer
Herman Brummer
6,414 Points

HI Guys,

What does this actually mean?

"In Python, attributes defined on the class, but not an instance, are universal. So if you change the value of the attribute, any instance that doesn't have it set explicitly will have its value changed, too!"

Damn been trying to faff out what this means, but I just don't get it.

Not sure if this is still relevant to you 2-1/2 months later, but this is the example Kenneth gave near the beginning of the classes lesson about the thief with the sneaky attribute.

If he sets the class default to sneaky = True, then every thief in the class will be sneaky. When creating an instance of the class, he can set them individually to False, but if he doesn't, they'll all be sneaky by default. Same is true if he were to set them to False at the class level. if he wanted only a few thieves to be sneaky, then he would set them to True will creating the instance.

Hope that helped :)

class RaceCar:

def __init__(self, color, fuel_remaining, laps = 0, **kwargs)    
    self.color = color
    self.fuel_remaining = fuel_remaining
    self.laps = laps
    for key, value in kwargs.items():
        setattr(self, key, value)

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

red_car = RaceCar(color='red', fuel_remaining=50, laps=10)

bright chibuike
bright chibuike
3,245 Points

try this code it will do the task: class RaceCar:

def __init__ (self,color,fuel_remaining=23,laps=0, **kwargs):
    self.laps = laps
    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 -= length*0.125
    self.laps += 1

mike =RaceCar(color ="white" ,fuel_remaining =24)

here is the right answer for me: class RaceCar: def init(self, color, fuel_remaining, laps=0, **kwargs): self.color = color self.fuel_remaining = fuel_remaining self.laps = laps for key, value in kwargs.items(): return setattr(self, key, value)