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 trialjalil damian
1,452 PointsChallenge Task 3 of 3 Great! One last thing. In Python, attributes defined on the class, but not an instance, are
i dont get it
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
20,315 Pointsclass 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
Josh Keenan
20,315 PointsSo 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?
jalil damian
1,452 Pointswhat do you mean?
Josh Keenan
20,315 PointsSo 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.
jalil damian
1,452 Pointsthat dint work
Josh Keenan
20,315 PointsSo you need help with the challenge?
jalil damian
1,452 Pointssord a
jalil damian
1,452 Pointsjalil damian
1,452 Pointstanks for your time
Josh Keenan
20,315 PointsJosh Keenan
20,315 Pointsdont forget to upvote and hit that best answer button and check out my merchandise, PEACE
jalil damian
1,452 Pointsjalil damian
1,452 Points谢谢