+ 2
I wanna check that my input is number or not.how can i code this?? (In phython)
input has a words nor number.
9 Respostas
+ 2
Could you suggest the language? A lot of languages have different ways to do it that.
+ 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
+ 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)
+ 2
you can use built in func type()
x = 1
print(type(x)==int)
output:
true
+ 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.
+ 1
but the problem is that x does't get an integer and also that statement result was true or false satement..🤔🤔
0
in phython
0
x = input("input ur number")
if isinstance(x, int)==True:
print(x)
else:
print("thats not number")
output:always print "thats not number" 😂
0
Using the ,,try'' block