0

error in simple calculator

while true: print("options") print("Enter 'add' to add two numbers") print("Enter 'substract' to substract two numbers") print("Enter 'multiply' to multiply two numbers") print("Enter 'divide' to divide two numbers") print("Enter 'quite' to end 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(result) elif user_input == "substract": num1 = float(input("Enter a number.")) num2 = float(input("Enter another number.")) result = str(num1 - num2) print( result) elif user_input == "multiply": num1 = float(input("Enter a number.")) num2 = float(input("Enter another number.")) result = str(num1 * num2) print( result) elif user_input == "divide": num1 = float(input("Enter a number.")) num2 = float(input("Enter another number.")) result = str(num1 / num2) print( result) else print("unknown input") not working

3rd Feb 2017, 11:10 PM
Fahim Hasan
Fahim Hasan - avatar
6 odpowiedzi
+ 3
Once time @jellyparakeet advices applied, code is working... but on SoloLearn code playground, there's still the limitation of in/ouput, and you cannot expect to real-time interract with your script: you need to fill ALL the inputs required by script execution just before running, each separated by a new line ^^ while True: # 'True' keyword is capitalized print("options") print("Enter 'add' to add two numbers") print("Enter 'substract' to substract two numbers") print("Enter 'multiply' to multiply two numbers") print("Enter 'divide' to divide two numbers") print("Enter 'quite' to end the program") # I suggest to correct the typo, as the word attended is 'quit' and not 'quite' 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(result) elif user_input == "substract": num1 = float(input("Enter a number.")) num2 = float(input("Enter another number.")) result = str(num1 - num2) print( result) elif user_input == "multiply": num1 = float(input("Enter a number.")) num2 = float(input("Enter another number.")) result = str(num1 * num2) print( result) elif user_input == "divide": num1 = float(input("Enter a number.")) num2 = float(input("Enter another number.")) result = str(num1 / num2) print( result) else: # need the colon ( double vertical dot ) to be proper written print("unknown input")
4th Feb 2017, 8:44 AM
visph
visph - avatar
+ 2
The "else" needs a colon so should be "else:". Also, the "true" in "while true" should be capitalized like "while True".
3rd Feb 2017, 11:51 PM
jellyparakeet
jellyparakeet - avatar
+ 2
Left13, why he did not put the "" in all his print statement is because he's calling a variable, in this case the variable is 'result' and you don't put "" when you want to print the value of a variable, the "" is used only to print out a string, so let's say he wrote print("result"), What you would see is not the answer to the question but the word 'result' will be printed on the screen because of the "", but without the "" you get the calculated value of the float you put there, the variable result which is the answer to the calculation will be printed.
4th Feb 2017, 10:40 AM
otokiniye
otokiniye - avatar
+ 1
It looks like a few of your print statements aren't indented correctly. Make sure they line up with the other statements in the if-else-if blocks. Python is a language where indentation matters.
3rd Feb 2017, 11:23 PM
jellyparakeet
jellyparakeet - avatar
+ 1
all the statements are rightly written on the code playground..but its still showing error
3rd Feb 2017, 11:36 PM
Fahim Hasan
Fahim Hasan - avatar
- 1
At all print(result) you forgot the "".So the right might be print("result")
3rd Feb 2017, 11:37 PM
lefteris13s
lefteris13s - avatar