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 Basic Object-Oriented Python Welcome to OOP Methods

Timothy Mattingly
Timothy Mattingly
15,934 Points

What happened to these method calls?

Theses method calls toward the end of the video: <car_one.stop()> <car_one.go('slow')> <car_one.go('fast')> <car_one.stop()> <car_one.stop()> seem to get overwritten when the use_gas method is introduced into the go method. I understand that the use_gas method is called automatically when <def go(self, speed)> is called but what happens to theses methods? The console prints 'The car is moving slow' but not 'The car is moving fast'.

1 Answer

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there, Timothy Mattingly! When we run those methods in that order, this is the output we get:

The car has already stopped.                                                                                                                                                                                              
The car starts moving.                                                                                                                                                                                                    
The car is going slow.                                                                                                                                                                                                    
You've run out of gas!                                                                                                                                                                                                    
The car has stopped.                                                                                                                                                                                                      
The car has already stopped. 

First, we call the .stop() method which prints out "The car has already stopped" because the is_moving attribute is currently False. Then we call the .go() method and pass in 'slow'. That method starts by calling the use_gas method. Because we didn't specify the amount of gas at the time we created the Car, it is given a value of 100 by default. The very first thing it does (before it does anything else), is subtract 50 from the current amount of gas. Now, self.gas has a value of 50. 50 is not less than or equal to 0 so it returns True and goes back to finish off the go() method. That first if will evaluate to True because that's what we returned from the use_gas() method. Remember, that at this point, the is_moving attribute is still False so we print out "The car starts moving." then set is_moving to True. Then we print out "The car is going slow", which is what we sent in.

That gives us the first 3 lines. Then we call .go() again and pass in 'fast'. Again, the go() method calls the use_gas method and again, the first thing it does is remove 50 from self.gas. But it was already 50 so now it's 0, so this time, the use_gas() method returns False instead of True and so the top if in the go() method does not fire this time. It would need to fire to print "The car is going fast". Instead, the else fires and says "You've run out of gas!", which is accurate because self.gas is now 0. That's the fourth line. But besides printing out that line, it also calls self.stop() on line 39, which makes sense since we're out of gas :smiley:

The stop() method being called from within go() results in the fifth line "The car has stopped." and is_moving is set to False. But then we call stop() again one last time on line 48. Because is_moving is now False from our previous call to stop() we get "The car has already stopped." That's the sixth line.

The reason we never see "The car is going fast", is because the second time we call go() the car is out of gas. Had you started with gas set to 250, you would get different results :smiley:

Hope this helps! :sparkles:

Timothy Mattingly
Timothy Mattingly
15,934 Points

Yes, that helps a lot! Thank you so much for the answer Jennifer. The stop method getting called by the instance and in the class threw me off and, I wasn't exactly sure where the line was getting returned for each time. Plus, I think the console in the video began to run the methods at the bottom line instead of at the top which made it appear as if if the first call got skipped or something. Ah, too many calls is tricky :grinning:

justlevy
justlevy
6,325 Points

Great explanation, thank you Jennifer Nordell.

It would be helpful to see the original code for reference.