Calculator in Python can't print out the result I want -


i wanted create calculator in python , had problems when printed out. problem everytime ran program printed out check errors. when removed it, started print me out numbers put together. example 1 + 2 = 12 or 2 + 5= 25, etc.this thing happened when tried add 2 numbers, when tried multiplying, subtracting or dividing didn't print out anything. code:

print ("enter first number") num1 = input() print("enter second number")  num2 = input() print("enter operation") operation = input() if operation "+": print(num1 + num2) elif operation "*": print(num1 * num2) elif operation "/": print(num1 / num2) elif operation "-": print(num1 - num2) else: print("check errors") 

@fjoni yzeiri: hi folk,

this common issue when starting python, don't declare variable type of input, save string, if concatenate (+ in python) concatenate 2 inputs.

to solve have explicitly cast values integers, means:

print ("enter first number") num1 = int(input()) # cast int here print("enter second number")  num2 = int(input()) # cast int here print("enter operation") operation = input() if operation "+": print(num1 + num2) elif operation "*": print(num1 * num2) elif operation "/": print(num1 / num2) elif operation "-": print(num1 - num2) else: print("check errors") 

of course, simple use case, if want learn bit more, try caught exception when tries cast non-integer string. teach nice stuff ;)


Comments