+ 1

How do you take an input in python and verify its type and print accordingly if it is integer, float or string?

for this , you need to take input and print the input is of type integer/string/float/something else

11th May 2017, 1:14 PM
Madhurima Chakraborty
Madhurima Chakraborty - avatar
5 odpowiedzi
+ 4
An input() entry in Python is always of string type ^^
11th May 2017, 1:18 PM
visph
visph - avatar
+ 4
you are right visph
11th May 2017, 1:19 PM
Vishnu ks
Vishnu ks - avatar
+ 3
def number(n): try: if '.' in n: n = float(n) else: n = int(n) except: pass finally: return n n = input() print(type(number(n))) # you can avoid the 'except' statement, but I put it for showing the complete 'try' structure ;)
11th May 2017, 2:30 PM
visph
visph - avatar
+ 1
yes i know. that is where the problem lies.. i got this question in a competition.. i could figure out a large method using try and except but i want a better solution
11th May 2017, 2:25 PM
Madhurima Chakraborty
Madhurima Chakraborty - avatar
0
nice thank you
11th May 2017, 3:45 PM
Madhurima Chakraborty
Madhurima Chakraborty - avatar