+ 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")
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
0
you are making too much mess . python is for simplicity . true is invalided . use True
0
That's literally the first I tried with python, could you explain with more detail please?
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.
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