+ 1

Can anyone tell me what is causing an error in this program? And how can I fix it?

while True : print ("Enter 'add' to add two numbers") print ("Enter 'subtract' to subtract two numbers") print ("Enter 'multiply' to multiply two numbers") print ("Enter 'divide' to divide two numbers") print ("Enter 'quit' to quite the program") user_input = input(": ") if user_input == "quit": break elif user_input == "add": num1 = float(input ("Enter a number:")) num2 = float(input ("Enter another number:" )) result = str(num1 + num2) print("The answer is " + result ) elif user_input == "subtract": num1 = float(input ("Enter a number:")) num2 = float(input ("Enter another number:")) result = str(num1 - num2) print("The answer is " + result ) elif user_input == "multiply": num1 = float(input ("Enter a number:")) num2 = float(input ("Enter another number:")) result = str(num1 * num2) print("The answer is " + result ) elif user_input == "divide": num1 = float(input ("Enter a number:")) num2 =

30th Apr 2020, 7:19 AM
Arif Ameer Hussain
Arif Ameer Hussain - avatar
4 Answers
+ 7
Regardless from all what was mentioned so far, you should ask yourself why to use multiple input procedures for each operation type. This is called DRY = Don't Repeate Yourself. Take out all input procedures and place only one in the top area where the other inputs are located. The same could be done with the output statement: Try to use only one in the bottom of the code.
30th Apr 2020, 10:00 AM
Lothar
Lothar - avatar
+ 4
The while True is fine. There is a break condition for when "quit" is entered. But, yes your indentation is off.
30th Apr 2020, 7:50 AM
ChaoticDawg
ChaoticDawg - avatar
+ 3
Check your identation for every print("The answer is" + result)
30th Apr 2020, 7:24 AM
RKK
RKK - avatar
+ 2
Your input syntax for num1 and num2 is wrong ! Follow this syntax throughout the code ; num1 = float(input ("Enter a number:"))
30th Apr 2020, 9:09 AM
$layer
$layer - avatar