crazy4s
asked on
How can i check whether the input is a number or string
Hi all,
I was trying out to write a small prog to prompt the user input for numbers...
I used the isdigit func to check whether the input is a number or string... it works but this doesn't accept float as a number... how can i do to make it works for both integer and float?
while True:
n1 = input("Enter the first number: ")
if n1.isdigit() == False:
print("Invalid input!")
else:
n1 = int(n1)
break
I was trying out to write a small prog to prompt the user input for numbers...
I used the isdigit func to check whether the input is a number or string... it works but this doesn't accept float as a number... how can i do to make it works for both integer and float?
while True:
n1 = input("Enter the first number: ")
if n1.isdigit() == False:
print("Invalid input!")
else:
n1 = int(n1)
break
Here is a link to the string doc you should really look at
http://docs.python.org/library/stdtypes.html
http://docs.python.org/library/stdtypes.html
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
Or you can use isnumeric instead of isdigit
ASKER
yes i know but it prints false for float...
ASKER
the isnumeric works the same as isdigit...
so i tried to reverse it...
it works for both integer and float but it gives an error if the string contains one or more integers and the rest are alphabet instead of printing "Invalid Input"...
Is there any other method to solve this prob?
while True:
n1 = input("Enter the first number: ")
if n1.isalpha() == True:
print("Invalid input!")
else:
n1 = float(n1)
break
so i tried to reverse it...
it works for both integer and float but it gives an error if the string contains one or more integers and the rest are alphabet instead of printing "Invalid Input"...
Is there any other method to solve this prob?
while True:
n1 = input("Enter the first number: ")
if n1.isalpha() == True:
print("Invalid input!")
else:
n1 = float(n1)
break
Well, you can certainly use RegEx if you want
from re import match
n.match("\d+(?:\.\d+)?")
from re import match
n.match("\d+(?:\.\d+)?")
ASKER
can you explain this more in details... and i couldn't find match attribute in my python ver 3.2.2... thanks:)
Sorry, I am trying to do it but I have fever so I apologize.
I am trying to use the regex library
You will have to import re or just match
from re import match
Then you will have to use the match function.
If you say
import re
You will have to use re.match()
Also, my syntax was wrong.
Give me a second let me give you the solution
I am trying to use the regex library
You will have to import re or just match
from re import match
Then you will have to use the match function.
If you say
import re
You will have to use re.match()
Also, my syntax was wrong.
Give me a second let me give you the solution
SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
>>>no = "123.456"
>>>from re import match
>>>no.match("\d+(?:\.\d+)? ")
Traceback (most recent call last):
File "<pyshell#30>", line 1, in <module>
no.match("\d+(?:\.\d+)?")
AttributeError: 'float' object has no attribute 'match'
>>>from re import match
>>>no.match("\d+(?:\.\d+)?
Traceback (most recent call last):
File "<pyshell#30>", line 1, in <module>
no.match("\d+(?:\.\d+)?")
AttributeError: 'float' object has no attribute 'match'
Sorry for being so slow
Here is what I have so far.
>>> b = re.match("\d+(?:\.\d+)?", "171.1")
>>> b.group(0)
'171.1'
>>> b = re.match("\d+(?:\.\d+)?", "171.a")
>>> b.group(0)
'171'
Here is what I have so far.
>>> b = re.match("\d+(?:\.\d+)?", "171.1")
>>> b.group(0)
'171.1'
>>> b = re.match("\d+(?:\.\d+)?", "171.a")
>>> b.group(0)
'171'
Here is something you may like
>>> a = "171.123"
>>> from re import match
>>> b = match("\d+(?:\.\d+)?", a)
>>> if a == b.group(0):
... print "A number"
...
A number
>>> a = "171.12a"
>>> b = match("\d+(?:\.\d+)?", a)
>>> if a == b.group(0):
... print "A number"
...
>>>
>>> a = "171.123"
>>> from re import match
>>> b = match("\d+(?:\.\d+)?", a)
>>> if a == b.group(0):
... print "A number"
...
A number
>>> a = "171.12a"
>>> b = match("\d+(?:\.\d+)?", a)
>>> if a == b.group(0):
... print "A number"
...
>>>
At this time, I would suggest you to go with gelonida's solution and save yourself a lot of time. Just make it a separate function like isFloat. And check isnumeric and isFloat and then you will be all good to go.
ASKER
Sorry for the late... N thanks for all the suggestions!
#!/usr/bin/python
str = "123456"; # Only digit in this string
print str.isdigit();
str = "this is string example....wow!!!";
print str.isdigit();