+ 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")

18th Jun 2018, 7:17 PM
King Dave
King Dave - avatar
4 odpowiedzi
+ 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
18th Jun 2018, 8:32 PM
Ipang
+ 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 : )
19th Jun 2018, 2:54 AM
Ipang
+ 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
18th Jun 2018, 8:50 PM
Pedro Demingos
Pedro Demingos - avatar
0
Ipang And how do you call the input?
18th Jun 2018, 11:34 PM
King Dave
King Dave - avatar