+ 3

How to check input type?

how to check user input type and if it isn't an integer show an error massage?

2nd Mar 2017, 10:55 AM
Geovim
Geovim - avatar
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!
2nd Mar 2017, 11:30 PM
Raul Maldonado
Raul Maldonado - avatar
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!
2nd Mar 2017, 12:04 PM
Raul Maldonado
Raul Maldonado - avatar
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
2nd Mar 2017, 12:10 PM
Twelfty
Twelfty - avatar
0
try: i = int(input()) except: print("input must be an integer") this should work
2nd Mar 2017, 3:29 PM
Yunus Kaya
Yunus Kaya - avatar
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!!!
2nd Mar 2017, 8:53 PM
Geovim
Geovim - avatar