0
class Car():
    def __init__(self,model,make,speed):
        self.__model = model
        self.__make = make
        self.__speed = speed

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

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

    def get_speed(self):
        return self.__speed


def main():

    speed = input("Enter your cars current speed:")
    make = input("Enter your cars make")
    model = input("Enter your cars model")
    input = input("Enter 1 to accelerate , 2 to brake, ")
    car = Car(model,make,speed)



    if input == 1:
        car.accelerate(speed)
    elif input == 2:
        car.brake(speed)

    self.__make = make
    self.__model = model
    self.__speed = speed

    speed1 = car.get_speed()
    print("Your cars make is:", make)
    print("Your cars model is:", model)
    print("Your cars speed:", speed)

main()

I don't know why its throwing an error: line 19, when I ask for speed input.

  • Add the traceback message, that makes it easier to spot. – tdelaney Aug 02 '21 at 01:48
  • 1
    `input = input(...)` -- no. Do not name a variable `input`. When you do that you're creating a local that shadows the _function_ named `input` so that function can no longer be accessed. – Charles Duffy Aug 02 '21 at 01:49
  • You have defined ```input``` as a variable. And you are accessing it before assignment. So an error. So don't use ```input```` as a variable because it is an inbuilt function. Instead, do ```inp=input(...)``` –  Aug 02 '21 at 01:49

1 Answers1

0

This line makes input a local function variable, shadowing the input function.

input = input("Enter 1 to accelerate , 2 to brake, ")

When python compiles a function, it turns local variables into slot indexes in the function object for speed. input is now a local variable for the duration for the function body. It can't also be the global "input". The solution is to use a different name for the variable.

def main():

    speed = input("Enter your cars current speed:")
    make = input("Enter your cars make")
    model = input("Enter your cars model")
    accelerate_or_brake = input("Enter 1 to accelerate , 2 to brake, ")
    car = Car(model,make,speed)
tdelaney
  • 73,364
  • 6
  • 83
  • 116