'Pycharm Error: Unresolved attribute reference 'isnumeric' for class 'int'
I was messing with a code that ask 2 numbers and let the user input what operation to do with them.
I need the input to be integers only, so i used .isnumeric, but i just can't make it go thru and it displays this error either i input numbers or letters.
if num1.isnumeric():
AttributeError: 'int' object has no attribute 'isnumeric'
Can some of you show me where is the problem?
here's the full code:
print('Você deverá digitar dois números e depois selecionar qual operação fazer com ambos.\\n')
while True:
num1 = int(input('Digite o primeiro número: '))
if num1.isnumeric():
break
else:
print("Apenas números, por gentileza")
while True:
num2 = int(input('Digite o segundo número: '))
if num2.isnumeric():
break
else:
print("Apenas números, por gentileza")
operador = input()
if operador == '+':
print('O resultado é:', num1 + num2)
elif operador == '-':
print('O resultado é:', num1 - num2)
elif operador == '*':
print('O resultado é:', num1 * num2)
elif operador == '/':
print('O resultado é:', num1 / num2)
else:
print('operador invalido')
I don't know what to try.
Solution 1:[1]
isnumeric() is a function of str. You need to remove the int from surrounding the input and move it to after you have verified that isnumeric() is True.
You also have incorrect indenting. I don't know if that was just an issue when pasting the code into StackOverflow, but to sum it up, after a line that ends with a colon (:), the next line needs to have an increased indent, continuing until the end of the selection/iteration code block.
Read more about indentation here.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | KingsMMA |
