Dec-13-2018, 05:45 PM
hi recently i started learning python by my self and i faced this problem :
how i can check the input if it int or a sting?
any help
how i can check the input if it int or a sting?
any help
how i can check the input type?
|
Dec-13-2018, 05:45 PM
hi recently i started learning python by my self and i faced this problem :
how i can check the input if it int or a sting? any help
Dec-13-2018, 06:27 PM
Try this
answer = input("Enter something: ") try: value = int(answer) print("it is an integer") except ValueError: print("it is not an integer")
Dec-13-2018, 06:59 PM
Dec-13-2018, 11:39 PM
I also use mostly try/except way proposed by @Gribouillis.
But look at the string methods isdecimal, isdigit, isnumeric. answer = input("Enter something: ") if answer.isnumeric(): number = int(answer) else: # do something else. |
|