As already mentioned in the other answers, the return of function input()
is the type string, the same applies to the function raw_input()
in Python 2.x.
numero1 = int(input("Informe um numero: "))
numero2 = int(input("Informe um numero: "))
Also consider treating possible exceptions which may occur, for example exception ValueError
which is thrown when a function receives an argument that has the right type, but an invalid value.
Behold:
try:
numero1 = int(input("Informe um numero: "))
numero2 = int(input("Informe um numero: "))
soma = numero1 + numero2
print ("{0} + {1} = {2}".format(numero1, numero2, soma))
except ValueError:
print("Somente numeros sao aceitos. Tente novamente.")
See demonstração
ut84, treating Value Error, example when setting int value for variable x only that a real value is inserted in this variable ? And thank you so much for your explanation.
– leogif
@leogif In this case, instead of
int
, should be usedfloat
. Behold that example.– stderr