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 trialTravis John Villanueva
5,052 PointsMaster Class 2/3
Ive tried and i dont know where is that bug.
- created a method run lap with length argument- Done
- decrease the fuel remaining by length * .125 - Done
- increment laps by 1 every time run_lap is called - Done
Im not sure do i need a return for it?
class RaceCar:
color=""
fuel_remaining=0
laps=0
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):
return self.fuel_remaining -= length * 0.125 and self.laps += 1
1 Answer
Steven Parker
231,269 PointsThe challenge doesn't require the method to return anything, you could but it's generally better to only do what the challenge asks for. However, I don't think you can make an assignment in a "return" statement.
Also, you may be expecting that "and" syntax to do something other than what it really does. Those two assignments should be separate statements.
Travis John Villanueva
5,052 PointsTravis John Villanueva
5,052 PointsHi Steven,
First of all, thank you for your prompt reply. It is Sunday here in Sydney and i salute you for answering me. Originally here is my code:
class RaceCar: color="" fuel_remaining=0 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.fuel_remaining -= length * 0.125 self.laps += 1
i was really on a hit and miss after this didnt work
Steven Parker
231,269 PointsSteven Parker
231,269 PointsIt's hard to tell without Markdown formatting, so it could be you had an indentation error. But if your "run_lap" method is structured like this, it should pass the challenge:
In future postings, use the instructions for code formatting in the Markdown Cheatsheet pop-up below the "Add an Answer" area. Or watch this video on code formatting.
Pitrov Secondary
5,121 PointsPitrov Secondary
5,121 PointsHi. Why do I have to do self.fuel_remaining and not just fuel_remaining? Because in the init I did self.fuel_remaining = fuel_remaining so that I could call it with fuel_remaining without the self.
Steven Parker
231,269 PointsSteven Parker
231,269 PointsI think you're misinterpreting what that line does.
self.fuel_remaining = fuel_remaining
just takes the value that was passed in through the parameter named "fuel_remainng" and assigns that value to the class field variable "self.fuel_remaining". You still need the "self." part to refer to that variable.