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

hector alvarado
hector alvarado
15,796 Points

IDK what's wrong with my class

the instruction of the code it's:

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 attr, value in kwargs.items():
            setattr(self, attr,value)

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

4 Answers

Oszkár Fehér
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Oszkár Fehér
Treehouse Project Reviewer

Hi Hector Your class looks okey. the methods are built well it's just one 'thing' actually self it's missing and a little typo

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

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

    def run_lap(self, length):
        fuel_remaining = fuel_remaining - (length * 0.125)       <--- from this line
        laps =+ 1             <--- and from this line     also the =+ it should be +=

When you use class attributes or functions it has to be called with the 'self' instance

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

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

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

I hope it helps. Happy coding

hector alvarado
hector alvarado
15,796 Points

OMG thanks, damit... The devil is in the detail right?

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

If I understand this problem, Laps is set to zero before the methods. Think about scope. Length is not made an instance since it’s used in the method run_lap only.