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

I'm a little confused... Please explain! [SOLVED]

So we have this code below.

    def __init__(self, model, year, make="Toyota"):
        self.make = make
        self.model = model
        self.year = year
        self.is_moving = False

    def stop(self):
        if self.is_moving:
            print("The car stopped")
            self.is_moving = False
        else:
            print("The car has already stopped")

    def go(self, speed):
        if not self.is_moving:
            print("The car starts moving")
            self.is_moving = True
        print(f"The car is going {speed}")

The is_moving is set to False but when we use it in the go() method we check to see if it's True with an if statement 'if not self.is_moving:' then we will set it to True. Its Set to False though so that code shouldn't run. So how does the if statement run if its set to False?

Thank you, Chris Freeman !

1 Answer

I think I figured it out. It's because we are checking not is_moving, which means if it is False then run the following code and set is_moving to True. Is that correct?