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

Darcie Kutryk
Darcie Kutryk
2,129 Points

Bummer! Try again... (but I don't know enough of what I don't know yet) RaceCar run_lap

Would anyone be able to point out what I'm missing in this practice problem? The Bummer/Try Again prompt doesn't give much to point out my error(s), and there isn't a preview pane for this example.

There are two aspects of new code added:

  • The laps attribute (set to zero). Added in init, but also tried outside of init (similar to sneaky = True in Kenneth's examples) without change in error message.
  • run_lap method defined with two parameters, self and length. Returned decreased fuel_remaining as well as increased lap... (can we even return two things?).

This module is going to take me much more review until I'm comfortable, so any additional suggestions are welcome.

racecar.py
class RaceCar:

    def __init__(self, color, fuel_remaining, laps, **kwargs):
        self.color = color
        self.fuel_remaining = fuel_remaining
        sel.laps = 0 # Add a laps attribute to the class, set to 0

        #Use setattr to take on any other keyword argument that comes in.
        for attribute, value in kwargs.items():
            setattr(self, attribute, value)

    # Add a method named run_lap, taking the length argument. 
    def run_lap(self, length):
        # Reduce fuel_remaining attribute by length multiplied by 0.125
        return self.fuel_remaining -= length(0.125)
        return self.laps += 1

2 Answers

Steven Parker
Steven Parker
231,236 Points

You've got the right idea, but a few syntax/implementation issues:

  • "laps" should not be an argument for "__init__"
  • "sel.laps" should be "self.laps" (with an "f")
  • only one "return" can be done in a method, and any code after it will never be performed
  • this method doesn't need to return anything
  • the syntax for multiplication requires a "*" operator
Darcie Kutryk
Darcie Kutryk
2,129 Points

Thanks so much, Steven. It helps a lot to know whether I'm tripping up on syntax (and spelling, clearly) versus if I'm missing concepts.

Just a some clarification for the init argument. 1 - Setting self.laps = 0 within init code body made sense as part of controlling how a class is created. Should it be outside of the init definition? 2 - When you say I don't need to include laps as an argument is it because it is handled by **kwargs

Steven Parker
Steven Parker
231,236 Points
  1. Setting self.laps = 0 is correct, it only had a spelling error
  2. "laps" is never passed to the new instance at all. It is created internally and initialized to 0.

Darcie,

I was banging my head against the wall on this one for a long time. Maybe you have also gotten past, but if not:

Laps does not need to be a part of the init line because it is not an attribute that you are going to assign to your race car, it is an action you are going to do with your already defined race car late in the run_lap method. So laps = 0 is a condition you want to set right under the creation of the class.

The time I got it to work I also wrapped the .125 in a float. I am not sure if that was necessary, but i'm also not sure how to go back and see if it was. Other than that your syntax looks the same other than removing the return as Steven suggested.