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

master class 3/3 Please help

Great! One last thing.

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

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

To prevent this, be sure to set the laps attribute inside the 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!

Chris Freeman
Chris Freeman
Treehouse Moderator 68,423 Points

Hi Trymore Matsapa, I'd be happy to help. What part of the instructions do you need help with? Can you show what your progress through Task 2 and what you tried for Task 3?

2 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,423 Points

Your latest code works with one correction:

:point_right: “set laps default value to 0”

The last three testing lines are not part of the class and can be omitted

Post back if you need more help. Good luck!!!

Master class 3/3

This is were i am having challenges

class RaceCar: def init(self,color,fuel_remaining,laps = 10,**kwargs): self.color = color self.laps = laps self.fuel_remaining = fuel_remaining

    for key,value in kwargs.items():
        setattr(self,key,value) 
def run_lap(self,length):
    self.fuel_remaining = self.fuel_remaining - (length * 0.125)
    self.laps = self.laps + 1

red_car = RaceCar("red", 50,0): RaceCar.laps = 10 red_car.laps = 10

Master class 2/3

This is what i did on 2/3 class RaceCar: def init(self,color,fuel_remaining,laps = 0,**kwargs): self.color = color self.laps = laps self.fuel_remaining = fuel_remaining

    for key,value in kwargs.items():
        setattr(self,key,value) 
def run_lap(self,length):
    self.fuel_remaining = self.fuel_remaining - (length * 0.125)
    self.laps = self.laps + 1

The bottom part is missing from the one i sent first.

class RaceCar: 
    def __init__(self,color,fuel_remaining,laps = 10,**kwargs):
        self.color = color
        self.laps = laps
        self.fuel_remaining = fuel_remaining

        for key,value in kwargs.items():
            setattr(self,key,value) 
    def run_lap(self,length):
        self.fuel_remaining = self.fuel_remaining - (length * 0.125)
        self.laps = self.laps + 1


red_car = RaceCar("red", 50,0):
RaceCar.laps = 10
red_car.laps = 10

[MOD: added ```python formatting -cf]