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

Karyna Reut
Karyna Reut
1,611 Points

How does self.gas -= 50 work?

Why -= 50? not 20? I don't understand this line, how does it work?

2 Answers

Steven Parker
Steven Parker
229,582 Points

The quantity was simply a design decision. The instructor chose to have the gas used in increments of 50 units. It could have just as easily been 20 or any other value.

The line works using the "subtractive assignment" operator (-=). It performs math and makes an assignment at the same time.

    self.gas -= 50            # this is a shorthand that does ...
    self.gas = self.gas - 50  # the same thing as this

the teacher chose 50 because our car was moving twice, it you set to 20 then you need to call the method 5 times