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

Challenge Task 3 of 3 Great! One last thing. In Python, attributes defined on the class, but not an instance, are

i dont get it

racecar.py
class RaceCar:
    laps = 0
    color = "Blue"
    fuel_remaining = 100

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

3 Answers

Josh Keenan
Josh Keenan
20,315 Points
class RaceCar:

    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)


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

Here is my solution, ask about any parts you don't understand

tanks for your time

Josh Keenan
Josh Keenan
20,315 Points

dont forget to upvote and hit that best answer button and check out my merchandise, PEACE

谢谢

Josh Keenan
Josh Keenan
20,315 Points

So in a class we are building a model of what we want an object to look like, so we don't necessarily want to pass in some values to it as it will be different for each object.

When we create an instance all we need to do is tell it what parameters to take and then we have already coded the blueprint, so it is made.

Does that answer your question and make sense?

what do you mean?

Josh Keenan
Josh Keenan
20,315 Points

So with a class we define what we want in the object we create, let's use a rectangle.

def __init__(self, length_one, length_two, color):

    self.length_one = length_one
    self.length_two = length_two
    self.color = color

In this fake init function we have created the plans for any rectangle we could possibly make. So one set of sides will be of length_one, and the other set of sides will be of length_two. Now we can set this on every rectangle we make so they can all be different. Then we can also set the color of each one with the color attribute.

that dint work

Josh Keenan
Josh Keenan
20,315 Points

So you need help with the challenge?

sord a