+ 3
How to check input type?
how to check user input type and if it isn't an integer show an error massage?
6 Answers
+ 1
I recommend you doing this, because it can also work with floats:
a=(input("Amount:"))
try: int(a)
except ValueError:
try: float(a)
except ValueError:
print ("This is not a number")
a=0
if a==0:
a=0
else:
print (a)
You can do operations in this way:
print(int(a)+2)
I hope this will help you!
0
hello Geovim!
You can do that doing something like a code I have done. Have a look at my profile and see the code.
I hope it will help you!
0
Raw input is String, and String has some methods to check its components.
For example, isdigit() returns if the string contains only digits.
s = input()
if s.isdigit():
print('integer')
else:
print('not integer')
There are also similar methods..
See https://docs.python.org/3/library/stdtypes.html
0
try:
i = int(input())
except:
print("input must be an integer")
this should work
0
i used a simple if already but the problem was when i write number for input in code playground , it considers the number as string!!!