Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Object-Oriented Python Instant Objects Master Class

can any one explain how to solve this?

Vrroom! OK, 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

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

    def run_lap(self,length):
        use_fuel = length * 0.125
        self.fuel_remaing-= use_fuel
        laps += 1
Jon Hockley
Jon Hockley
3,781 Points

Rather than tell you the correct answer:

  1. You have a typo in the run_lap method.
  2. The way you are incrementing the 'laps' isn't quite right. Have a look around the videos again, or search Google on how to change a class attribute in Python.

Good luck!

4 Answers

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

You forget to add self before laps attribute in the run_lap method

Kyle Salisbury
seal-mask
.a{fill-rule:evenodd;}techdegree
Kyle Salisbury
Full Stack JavaScript Techdegree Student 16,347 Points

wont matter too much since length is spelled the same through out your code but spelling length correctly might affect some other peoples 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.fuel_remaining -= length * 0.125 self.laps += 1

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