+ 1

Python simple calculator not working, anybody can help?

while True: print("Options") print("Enter '+' for addition") print("Enter '-' for subtraction") print("Enter '*' for multiplication") print("Enter '/' for division") print("To exit please enter 'E'") user_input=input(":") if user_input == "E": break elif user_input == "+": num1 = float(input("Enter a number")) num2 = float(input("Enter another number")) result = num1 + num2 print("The answer is " + result) elif user_input == "-": num1 = float(input("Enter a number")) num2 = float(input("Enter another number")) result = num1 - num2 print("The answer is " + result) elif user_input == "*": num1 = float(input("Enter a number")) num2 = float(input("Enter another number")) result = num1 * num2 print("The answer is " + result) elif user_input == "/": num1 = float(input("Enter a number")) num2 = float(input("Enter another number")) result = num1 / num2 print("The answer is " + result) else: print("Unknown input!") anybody can help with my code? i couldnt run it as a calculator.. not sure which part is wrong.. thanks in advanced!!!

31st May 2020, 9:30 AM
Pei Ying
Pei Ying - avatar
11 odpowiedzi
+ 6
That line at 8 keeps accepting input from user and you need to give all the inputs at once in sololearn + 5 6 E // to break out of loop
31st May 2020, 10:15 AM
Abhay
Abhay - avatar
+ 3
Check these statements print("The answer is " + result) By using + you are concatenating string and float which results in error ,instead it should be print("The answer is"+str(result)) Or print("The answer is ",result) Or print(f"the answer is {result}") Or print("The answer is {0}".format(result))
31st May 2020, 9:39 AM
Abhay
Abhay - avatar
+ 3
You can give unlimited inputs here as well but it isn't interactive like in pydroid3 ,
31st May 2020, 10:30 AM
Abhay
Abhay - avatar
+ 2
You should convert the float result to string, like this: while True: print("Enter '+' for addition") print("Enter '-' for subtraction") print("Enter '*' for multiplication") print("Enter '/' for division") print("To exit please enter 'E'") user_input=input(":") if user_input == "E": break elif user_input == "+": num1 = float(input("Enter a number")) num2 = float(input("Enter another number")) result = num1 + num2 print("The answer is " + str(result)) elif user_input == "-": num1 = float(input("Enter a number")) num2 = float(input("Enter another number")) result = num1 - num2 print("The answer is " + str(result)) elif user_input == "*": num1 = float(input("Enter a number")) num2 = float(input("Enter another number")) result = num1 * num2 print("The answer is " + str(result) ) elif user_input == "/": num1 = float(input("Enter a number")) num2 = float(input("Enter another number")) result = num1 / num2 print("The answer is " + str(result))
31st May 2020, 12:56 PM
CLAD
CLAD - avatar
+ 1
@kiibo ghayal hi, below is the link of my python calculator https://code.sololearn.com/cI0i4ar9MqfS/#py
31st May 2020, 10:12 AM
Pei Ying
Pei Ying - avatar
0
@Abhay hi, i just edited to print("The answer is ",result) and key in the input as : + 2 2 but below is the output i get: Options Enter '+' for addition Enter '-' for subtraction Enter '*' for multiplication Enter '/' for division To exit please enter 'E' :Enter a numberEnter another numberThe answer is 4.0 Options Enter '+' for addition Enter '-' for subtraction Enter '*' for multiplication Enter '/' for division To exit please enter 'E' : Traceback (most recent call last): File "./Playground/file0.py", line 8, in <module> user_input=input(":") EOFError: EOF when reading a line may i know why line 8 is wrong???
31st May 2020, 9:45 AM
Pei Ying
Pei Ying - avatar
0
@Abhay wow thanks alot, finally i got it!! @Kiibo Ghayal do you mean using the same code in Pydroid3 or Qpython, can do for more inputs?
31st May 2020, 10:27 AM
Pei Ying
Pei Ying - avatar