+ 1

Why does my calculator not work?

can someone tell me whats wrong? while true: 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=='substract' num1=float(input("Enter a number:")) num2=float(input("Enter another number:")) result=str(num1-num2) print("The answer is"+result) (also wrote the codes for division and multiple,too much to type though) else: print("Unknown input")

9th Dec 2016, 4:35 PM
Franny
Franny - avatar
5 Réponses
+ 2
Incorrect Code: while true: #true is a variable, use True (bool) user_input=input() if user_input=="quit" #colon missing. 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=='substract' num1=float(input("Enter a number:")) num2=float(input("Enter another number:")) result=str(num1-num2) print("The answer is"+result) Corrected Code: while True: user_input=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=='substract': num1=float(input("Enter a number:")) num2=float(input("Enter another number:")) result=str(num1-num2) print("The answer is"+result) So in-short: Remember: a) Indentation is NOT for beauty, it just has to be done in order for python to be able to interpret it b)colon after a statement is important c)don't get confused between a Boolean Literal and a variable. And please let me know if you have any querries. and never lose faith in the power of python. Regards Aditya Shukla
10th Dec 2016, 3:28 PM
Aditya Shukla
Aditya Shukla - avatar
0
you are making too much mess . python is for simplicity . true is invalided . use True
9th Dec 2016, 5:09 PM
Sun
Sun - avatar
0
That's literally the first I tried with python, could you explain with more detail please?
9th Dec 2016, 7:36 PM
Franny
Franny - avatar
0
Not to be Offensive, but this Python Code is Messy. Use indents by TAB'ing in appropiate places, to make easy readability. Also, consider the appropiate spelling of Python Functions. Remember: Python is a CaSe SeNsItIvE Language! Good Luck.
9th Dec 2016, 8:02 PM
ghostwalker13
ghostwalker13 - avatar
0
see my latest Basic Calculator code its the easiest for a calculator and least messy and the function which I used is really known to a very little people
19th Dec 2016, 1:35 PM
Bruh