+ 2
If input equals var type
if input == int: print ("input is an integer") elif input == float : print ("input is a float")do I need to do something special when calling the input? elif input == string: print ("input is a string") else: print ("input undefined")
4 Antworten
+ 7
if type(var) is int:
# var is integer
elif type(var) is float:
# var is float
elif type(var) is str:
# var is string (default)
(* Edited *)
Hth, cmiiw
+ 4
King Dave what do you mean call the input? to take user input you use input() function. I think you better use a different name for the input buffer variable, so not to confuse between the variable and input() function.
You can use int() or float() function to convert the return value from input() function of which is string.
See Pedro Demingos answer for validating input, that is a good example given : )
+ 2
Just note that the input() function will always return you a string. So if you wanna check if it’s a number, you should do something like:
inp = input()
try:
n = float(inp)
# is a number
except TypeError:
pass
# not a number
0
Ipang And how do you call the input?