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

Clarification on use_gas method

Trying to get a better understanding of the use_gas method below:

def use_gas(self):
        self.gas -= 50
        if self.gas <= 0:
            return False
        else:
            return True

I think we are using booleans for the same logic as to why we use the "if not" in the go method:

def go(self, speed):
        if self.use_gas():
            if not self.is_moving:
                print('The car has started moving')
                self.is_moving = True
            print(f'The car is going {speed}')
        else:
            print('You have run out of gas')
            self.stop()

If I'm understanding this correctly, the go method is checking if "self.use_gas" is truthy? So if it's True it will run the "if not self.is_moving" but if it's false (ie not truthy) we print you are out of gas. Is this the correct logic?

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,454 Points

Hey Kylie Nonemaker, you have it correct.

Post back if you need more help. Good luck!!!