I am trying to make a simple calculator but i get an error after the input. Please help | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

I am trying to make a simple calculator but i get an error after the input. Please help

I am trying to make a simple calculator but i get an error after the input. Please help!!! this is the code: while True: print("Options:") print("Type Add to add values") print("Type Subtract to subtract values") print("Type Multiply to multiply values") print("Type Divide to dividde values") print("Type Quit to exit the calculator") print("Choose") user_input = "Quit" user_input1 = "Add" user_input2 = "Subtract" user_input3 = "Multiply" user_input4 = "Divide" if user_input == "Quit": break elif user_input1 == "Add": num1 = float(input("Enter a value")) num2 = float(input("Enter second value")) result = str(num1 + num2) print ("The answer is " + result) elif user_input2 == "Subtract": num1 = float(input("Enter a value")) num2 = float(input("Enter second value")) result = str(num1 + num2) print ("The answer is " + result) elif user_input3 == "Multiply": num1 = float(input("Enter a value")) num2 = float(input("Enter a ssecond value")) result = str(num1 + num2) print("The answer is " + result) elif user_input4 == "Divide": num1 = float(input("Enter a value")) num2 = float(input("Enter another value")) result = str(num1 + num2) print("The answer is " + result)

17th Feb 2017, 1:22 PM
Olamide Odedeji
Olamide Odedeji - avatar
7 Answers
+ 3
You can use only one variable to store the user input for operation, and have it receive the input as well: user_input = input("Choose: ") Also, keep in mind that as far as Python is concerned, 'Add' ! = 'add', so even if your user type 'add', it will not pass the adding condition. A nice workaround for this is to have user_input = user_input.lower() That will convert the user input to lower case, and then you adapt your conditions to accept lower case. if user_input == "quit": This way, the program will accept 'quit', 'Quit', 'QUIT', 'QuIT', and so on.
17th Feb 2017, 1:55 PM
Rodrigo
Rodrigo - avatar
+ 3
while True: print("Options:") print("Type Add to add values") print("Type Subtract to subtract values") print("Type Multiply to multiply values") print("Type Divide to dividde values") print("Type Quit to exit the calculator") user_input = input("Choose:") user_input.lower() if user_input == "Quit": break elif user_input == "Add": num1 = float(input("Enter a value")) num2 = float(input("Enter second value")) result = str(num1 + num2) print ("The answer is " + result) elif user_input == "Subtract": num1 = float(input("Enter a value")) num2 = float(input("Enter second value")) result = str(num1 + num2) print ("The answer is " + result) elif user_input == "Multiply": num1 = float(input("Enter a value")) num2 = float(input("Enter a ssecond value")) result = str(num1 + num2) print("The answer is " + result) elif user_input == "Divide": num1 = float(input("Enter a value")) num2 = float(input("Enter another value")) result = str(num1 + num2) print("The answer is " + result) break try this and lets see
17th Feb 2017, 2:46 PM
Elijah Arhinful
Elijah Arhinful - avatar
+ 2
I get your very same error if I use Python 2.7 instead of Python 3.X. So, which version of Python are you using?
17th Feb 2017, 2:31 PM
Stefano Casali
Stefano Casali - avatar
+ 1
while True: print("Options:") print("Type Add to add values") print("Type Subtract to subtract values") print("Type Multiply to multiply values") print("Type Divide to dividde values") print("Type Quit to exit the calculator") user_input = input("Choose: ") user_input = user_input.lower() if user_input == "quit": break elif user_input == "add": num1 = float(input("Enter a value")) num2 = float(input("Enter second value")) result = str(num1 + num2) print ("The answer is " + result) elif user_input == "subtract": num1 = float(input("Enter a value")) num2 = float(input("Enter second value")) result = str(num1 + num2) print ("The answer is " + result) elif user_input == "multiply": num1 = float(input("Enter a value")) num2 = float(input("Enter a ssecond value")) result = str(num1 + num2) print("The answer is " + result) elif user_input == "divide": num1 = float(input("Enter a value")) num2 = float(input("Enter another value")) result = str(num1 + num2) print("The answer is " + result) The above runs fine for me on IDLE. Compare it with what you have.
17th Feb 2017, 2:19 PM
Rodrigo
Rodrigo - avatar
+ 1
That must be it. The code I provided must be used in Python 3.
17th Feb 2017, 8:11 PM
Rodrigo
Rodrigo - avatar
0
it returns this even after the correction! Traceback (most recent call last): File "C:/Users/hp/Desktop/My simple Calculator", line 9, in <module> user_input = input("Choose: ") File "<string>", line 1, in <module> NameError: name 'Add' is not defined
17th Feb 2017, 2:10 PM
Olamide Odedeji
Olamide Odedeji - avatar
0
I use 2.7 python
17th Feb 2017, 7:48 PM
Olamide Odedeji
Olamide Odedeji - avatar