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

Kabir Gandhiok
Kabir Gandhiok
12,553 Points

Not sure how to do the second part of this challenge: increment an attribute everytime a method is called

I tried adding while and for loops, tried using if statements to increment laps by 1 everytime run_lap() is called, tried using self.class.laps +=1 which I got from google, but nothing seems to work, what am I missing or doing wrong?

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):
        if self.fuel_remaining:
            fuel_remaining - (length * 0.25)
Kabir Gandhiok
Kabir Gandhiok
12,553 Points

I fixed the code above and I think it passes in the shell based on what output i get, yet I dont know why it doesnt pass in the challenge, here's what i did:

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

on output i get:

x = racecar.RaceCar("pink", 10) # fuel_remaining = 10 x.fuel_remaining 10 x.run_lap(10) 8.75 x.laps 1 x.run_lap(10) #calling method again 8.75 # 10 - (10 * 0.125) ....am I getting the math wrong? x.laps 2 # is it not incrementing by 1, yet challenge isnt passing... x.laps 2 x.run_lap(10) 8.75 x.laps 3

1 Answer

Steven Parker
Steven Parker
231,269 Points

You're close, but I spot a few issues:

  • you wrote "salf" (with an "a") instead of "self" where you increment laps
  • you modified length instead of fuel_remaining when you recalculate the fuel
  • the challenge did not ask for a length property
  • run_lap doesn't need to return anything
Kabir Gandhiok
Kabir Gandhiok
12,553 Points

Hey, thanks Steven! I modified fuel_remaining and now it works!