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

What is wrong with my code?

now let's add a method named run_lap. It'll take a length argument. It should reduce the fuel_remaining attribute by length multiplied by 0.125. Oh, and add a laps attribute to the class, set to 0, and increment it each time the run_lap method is called.

racecar.py
class RaceCar:
    #laps=0
    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
Ivan Frances Alcantara
Ivan Frances Alcantara
4,528 Points

I copy and paste Amnah's code and it accepts it as correct. Does self.laps=0 already create a keyvalue argument through **kwargs?

11 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,426 Points

You are on the right path. In addition to assigning a value to self.laps, the code needs to allow the laps to be set to a specific value.

  • Add a laps keyword to the __init__ method. Include a default value. The value zero you used before works fine.
  • set self.laps to the laps argument

Post back if you need more help. Good luck!

Pratham Patel
Pratham Patel
4,976 Points

I tried what you said with her code but i still got errors

class RaceCar:
    def __init__(self,color,fuel_remaining,**kwargs, laps=0):
        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

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

Chris Freeman
Chris Freeman
Treehouse Moderator 68,426 Points

Pratham Patel, you are very close! The laps keyword assignment must come before the generic kwargs catch-all parameter.

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=self.laps+1

This code works!!

why should the length not be self.length ?

Chris Freeman
Chris Freeman
Treehouse Moderator 68,426 Points

The perimeter length is a local variable to the method run_lap. It is not an attribute to the class RaceCar. As a local variable, it may be accessed directly and is not referenced as an attribute.

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

Chad Goldsworthy
Chad Goldsworthy
4,209 Points

Just for anyone still struggling with this, I think I managed to find the issue. I tried all the code other people posted and said worked, but it did not work for me (and it seems there were many people having the same issue).

I ran it through my IDE and realised the length*0.125 in the run_lap method was causing a TypeError: unsupported operand type(s) for -=: 'str' and 'float'

So, when assigning fuel_remaining in the init method, you should convert it to a float as well, like: self.fuel_remaining = float(fuel_remaining)

I say "you should" lightly though, I'm no expert but it's just what fixed the issue for me.

This is the code that eventually worked for me:

class RaceCar:

    laps = 0

    def __init__(self, color, fuel_remaining, **kwargs):

        self.color = color
        self.fuel_remaining = float(fuel_remaining) # <-- this is what fixed it for me, 
                                                    #     adding the float() method
        for key, value in kwargs.items():
            setattr(self, key, value)

    def run_lap(self, length):
        self.fuel_remaining -= (length * 0.125)
        self.laps += 1
Chris Freeman
Chris Freeman
Treehouse Moderator 68,426 Points

Using float() shouldn’t be necessary. I suspect the TypeError was caused by the arguments chosen during your IDE testing. As is, this code doesn’t pass the challenge. It’s missing the lap parameter in the __init__ method.

Not having any luck with this code. What am I doing wrong?

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

I also tried this but no luck either:

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 -= self.length * 0.125
        laps += 1
Chris Freeman
Chris Freeman
Treehouse Moderator 68,426 Points

In the first code sample

# should be
self.fuel_remaining -= length * 0.125

# instead of 
self.fuel_remaining -= self.length * 0.125

Same for the second code sample, plus

    # should be added to __init__ 
    self.laps = 0

    # in run_lap should be 
    self.laps += 1

    # instead of
    laps += 1

Why this doesn't pass?

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

ok got it, I missed self.fuel_remaining....

Could someone explain in detail what exactly self does?

Chris Freeman
Chris Freeman
Treehouse Moderator 68,426 Points

In a class method, the parameter self gets assigned to the instance that is being referenced so that the code knows where to find the attribute to update.

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_laps(self, length):
                self.fuel_remaining -= self.length * 0.125
                self.laps = self.laps + 1

Don't really get what is wrong here! Please advise

Chris Freeman
Chris Freeman
Treehouse Moderator 68,426 Points

You are very close.

  • The method name should be run_lap not run_laps
  • refer to the parameter length and not the non-existent attribute self.length

Post back if you need more help! Good Luck!!

Chris Freeman how come here we can the length attribute as just "length" opposed to "self.length" like we have with every other attribute?

Sharing the correct code:

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.laps += 1
        self.fuel_remaining -= length * 0.125
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):
        #reduce the result of the **
        laps += 1
        self.fuel_remaining -= length * 0.125

Do you guys see any issues???

Chris Freeman
Chris Freeman
Treehouse Moderator 68,426 Points

You're VERY close! change laps += 1 to self.laps += 1

Post back if you need more help. Good luck!

Fixed