If input equals var type | Sololearn: Learn to code for FREE!


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

6/18/2018 7:17:43 PM

King Dave

4 Answers

New Answer


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


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 : )


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


Ipang And how do you call the input?