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

jimvichos
jimvichos
4,700 Points

I dont know whats wrong probably a logical mistake but i need help! Task 2-3 master-class

I dont know whats wrong probably a logical mistake but i need help! Thats my code and i dont know whats wrong

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

1 Answer

Alex Koumparos
seal-mask
.a{fill-rule:evenodd;}techdegree
Alex Koumparos
Python Development Techdegree Student 36,887 Points

Hi Jimvichos,

Your problem is with the laps attribute.

The challenge wants you to add a laps attribute to the class and set it to zero. You've done this in the initialiser, which is totally valid but the way you've done this has changed the initialiser's signature.

By adding laps as a parameter to your initialiser, it is now only possible to create a RaceCar instance by providing a laps value (even though that value is ignored in the body of the initialiser where you just set it to zero).

Before you made this change, you would create a RaceCar as follows:

RaceCar(color='red', fuel_remaining=2)

But now, that same instantiation will crash because you haven't supplied a laps value.

If you want to make laps an optional parameter in your initialiser you can give it a default value and then set the instance's attribute from the initialiser's body:

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)

This will enable you to create your RaceCar just like you could before:

RaceCar(color='red', fuel_remaining=2)

Will make a new red car with zero laps on the clock. But leave you the possibility of dropping in a new car partway through the race:

RaceCar(color='yellow', fuel_remaining=2, laps=5)

Hope that clears everything up.

Cheers

Alex