0

Why am I getting an error?

I’m trying to make a simple calculator. In the last line, I get an error saying “result” is not defined. How do I fix this? while True: print("type 'add' to add two numbers") print("type 'subtract' to subtract two numbers") print("type 'divide' to divide two numbers") print("type 'multiply' to multiply two numbers") print("type 'quit' to end program") user_input=input("operation:") print(user_input) if user_input==("quit"): break num1=float(input("number 1:")) print(num1) num2=float(input("number 2:")) print(num2) if input==("add"): result=str(num1+num2) elif input==("subtract"): result=str(num1-num2) elif input==("multiply"): result=str(num1*num2) elif input==("divide"): result=str(num1/num2) print("answer:"+result)

21st May 2020, 7:27 PM
•—• • •-• ••• —- -•
•—• • •-• ••• —- -• - avatar
2 odpowiedzi
+ 1
You named the operation input user_input but in the if-elif-part you write input. And there is no need to put "add" and so on in a tuple, right?
21st May 2020, 7:38 PM
Lisa
Lisa - avatar
0
In the if statements you write "if/elif input==("operation") , but "input" doesn't exist only exist "user_input", change this, and is unnecessary write the parentheses
21st May 2020, 7:37 PM
4lx
4lx - avatar