+ 2

I wanna check that my input is number or not.how can i code this?? (In phython)

input has a words nor number.

25th Oct 2018, 12:37 PM
Ashen Weligalla
Ashen Weligalla - avatar
9 Answers
+ 2
Could you suggest the language? A lot of languages have different ways to do it that.
25th Oct 2018, 12:41 PM
Anya
Anya - avatar
+ 5
Sorry i'm posting again because i miss read. If you only want to know if is numeric you can use isnumeric method. See a example: https://code.sololearn.com/c394h4JJtJT4
25th Oct 2018, 1:21 PM
Anya
Anya - avatar
+ 3
So this is for Python3 (is a little different from Python2) You can use the Bult-in function ininstance Syntax in simple way: ininstance(variable, type) Here one example using isinstance: https://code.sololearn.com/cndrtdIVS8fW/#py For more reference about this you can access and read more about isinstance. https://code.sololearn.com/cndrtdIVS8fW/#py (Python Documentation)
25th Oct 2018, 1:16 PM
Anya
Anya - avatar
+ 2
you can use built in func type() x = 1 print(type(x)==int) output: true
25th Oct 2018, 1:22 PM
Mbrustler
Mbrustler - avatar
+ 2
As suggested by Anya, if x.isnumeric(): would work in your code, but only if it's an integer >=0. I normally just try to convert it to an integer, and if it fails, I know that it isn't one 😂 x = input() try: print(int(x)) # Or, if you like, x = int(x) except: print("That's not an integer.") Similarly for floats.
25th Oct 2018, 2:22 PM
Kishalaya Saha
Kishalaya Saha - avatar
+ 1
but the problem is that x does't get an integer and also that statement result was true or false satement..đŸ€”đŸ€”
25th Oct 2018, 1:46 PM
Ashen Weligalla
Ashen Weligalla - avatar
0
in phython
25th Oct 2018, 12:46 PM
Ashen Weligalla
Ashen Weligalla - avatar
0
x = input("input ur number") if isinstance(x, int)==True: print(x) else: print("thats not number") output:always print "thats not number" 😂
25th Oct 2018, 1:49 PM
Ashen Weligalla
Ashen Weligalla - avatar
0
Using the ,,try'' block
11th Nov 2018, 11:28 AM
kritomas
kritomas - avatar