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

mohan Abdul
PLUS
mohan Abdul
Courses Plus Student 1,453 Points

O.O.P python OK, now let's add a method named run_lap. It'll take a length argument. It should reduce the fuel_remaining

something isn't really clicking together heres second try (Steven Parker, could you please help or any one else please?) :

class RaceCar:

    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, fuel_remaining, total_length, total):
        count = 0
        self.laps = self.run_lap
        count = +1
        self.length = self.length
        self.total_length = self.length * 0.125
        self.total = self.length - self.fuel_remaining
racecar.py
class RaceCar:

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

1 Answer

Steven Parker
Steven Parker
231,236 Points

Here's a few hints:

  • you won't need "count" ("laps" does that job)
  • but you should create "laps" with the other instance variables, and set it to 0 there
  • "length" is a parameter, not an instance variable (so no "self.length")
  • you won't modify "length" , but you will use it to modify "fuel_remaining"
  • make sure the fuel formula matches the instructions
mohan Abdul
mohan Abdul
Courses Plus Student 1,453 Points

this is what i got so far, with self.total_length and self.total_fuelremaining i get this error message, "AttributeError: 'RaceCar' object has no attribute 'total_length'." even if i take away the instance variable self.

class RaceCar:

    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.laps = 0
        length = length
        self.total_length = length * 0.125
        self.total_fuel = total_length - self.fuel_remaining
mohan Abdul
mohan Abdul
Courses Plus Student 1,453 Points

why doesn't length need an instance variable. in the initfunction method color and fuel_remaining all got given an instance variable.

Steven Parker
Steven Parker
231,236 Points

A few more hints:

  • "total_length" is not something the instructions tell you to keep track of
  • "fuel_remaining" is the name of the fuel variable, not "total_fuel"
  • "length" is a parameter because it is also not kept after being used to calculate "fuel_remaining"
  • you should create "laps" in the "__init__" method with the other instance variables
  • assigning a variable to itself doesn't do anything useful
mohan Abdul
mohan Abdul
Courses Plus Student 1,453 Points

The last two lines don't do anything.

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):
        length * 0.125                            #doesn't do anything 
        self.fuel_remaining - length     #doesn't do anything
Steven Parker
Steven Parker
231,236 Points

You're right, just performing a calculation doesn't do anything lasting. For that, you need to perform the calculation in an assignment statement that puts the result into a variable (perhaps back into one being used in the calculation).

mohan Abdul
mohan Abdul
Courses Plus Student 1,453 Points

i got the last two lines to do something but i still get a bummer message "Bummer: Did you adjust the fuel_remaining attribute?"

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):
        total_fuel = (int(length) * 0.125) - (int(self.fuel_remaining) - length)
        return total_fuel
Steven Parker
Steven Parker
231,236 Points

Sure enough, this code makes no changes to "fuel_remaining" but creates and returns "total_fuel" instead. But that's not what the instructions ask for. So to conform to the instructions:

  • you don't need to create a new variable
  • the fuel calculation should put the result back into "fuel_remaining"
  • you don't need "int" conversions, these values are already ints
  • "length" should be used only once in the formula
  • this method also needs to increase "self.laps" (in a separate statement)
  • you don't need to "return" anything
mohan Abdul
mohan Abdul
Courses Plus Student 1,453 Points

With out return nothing happens, can you show me how to do it with out return? I have no idea how create a count for self.lap do i use the enumerate or f' string index? . I neatened the formula so only length is used once but i have encountered another problem, if enter the following in the console it only takes 1. 25 away: >>> mohan=RaceCar("blue", 56)

mohan.run_lap(10)
54.75.

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 = (self.fuel_remaining) - (length * 0.125)
        return self.fuel_remaining
Steven Parker
Steven Parker
231,236 Points

The fuel calculation seems correct now. I see how you are using "return" for testing, but it's not needed for the challenge.

The code for a typical method to increment the laps would look like this:

        self.laps += 1
mohan Abdul
mohan Abdul
Courses Plus Student 1,453 Points

'''self.laps=0''' has to be set to zero and increment by one from there, so does it look like this: (it comes back with "Bummer: Be sure to set the laps attribute to 0")

class RaceCar:

    def __init__(self, color, fuel_remaining, **kwargs):
        self.color = color
        self.fuel_remaining = fuel_remaining
        self.laps = 0
        self.laps += 1 
Steven Parker
Steven Parker
231,236 Points

Setting it to 0 is done in the "__init___" method. But the increment should be done only in the "run_lap" method.

mohan Abdul
mohan Abdul
Courses Plus Student 1,453 Points

I tried to put it through and it comes back with, "Bummer: Hmm, some attributes didn't get set correctly".

class RaceCar:

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


    def run_lap(self, length):
        self.laps += 1
        self.fuel_remaining = (self.fuel_remaining) - (length * 0.125)
Steven Parker
Steven Parker
231,236 Points

Your original code had some lines in the "__init__" method that set additional keyword arguments using "setattr". But apparently during the other changes, that bit of the code got removed.

Put it back in and you should pass the challenge.