Creating class using python (help with code)

The brake method is suppose to subtract 5 from the speed data attribute but it’s showing up as -5 instead.
here’s my code below:

class Car:
    def __init__(self,__year_model, __make):
        self.__year_model ==__year_model
        self.__make == __make
        self.__speed == 0


    def accelerate(self):
        self.__speed =+5


    def brake(self):
        self.__speed = -=5

    def get_speed(self):
        return self.__speed

```from car import Car
my_car = Car("Corvette", 2022)

for a in range(0,5):
     b = my_car.accelerate()
     speed = my_car.get_speed()
     print(f"accelerate {a}: {speed}")


print()


for x in range(0,5):
    ac = my_car.brake()
    speed = my_car.get_speed()
    print(f"brake {x}: {speed}")
    
accelerate 0: 5
accelerate 1: 10
accelerate 2: 15
accelerate 3: 20
accelerate 4: 25

brake 0: -5
brake 1: -5
brake 2: -5
brake 3: -5
brake 4: -5

**type or paste code here**

This:

 x = 5   # sets x to 5
 x = -5  # sets x to -5
 x =-5   # also sets x to -5 because the spaces are optional
 x = +5  # sets x to 5
 x =+5   # also sets x to 5 because the spaces are optional

 x += 5  # adds 5 to x
 x -= 5  # subtracts 5 from x

Basicly you’ve mistyped -= and/or += which have to be in that order.

2 Likes
    def accelerate(self):
        self.__speed =+5

    def brake(self):
        self.__speed = -=5

    b = my_car.accelerate()

    ac = my_car.brake()

Neither of those methods returns anything, so b and ac don’t actually have a value. Using in-place operations (+= or -=) means you are modifying the variable directly, so you don’t have to set them as = to anything; just call them directly.

    def accelerate(self):
        self.__speed += 5

    def brake(self):
        self.__speed -= 5

    my_car.accelerate()

    my_car.brake()