Link to home
Create AccountLog in
Avatar of crazy4s
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
Avatar of farzanj
farzanj
Flag of Canada image

Here is an example for you

#!/usr/bin/python


str = "123456";  # Only digit in this string
print str.isdigit();

str = "this is string example....wow!!!";
print str.isdigit();
Here is a link to the string doc you should really look at
http://docs.python.org/library/stdtypes.html
ASKER CERTIFIED SOLUTION
Avatar of farzanj
farzanj
Flag of Canada image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Or you can use isnumeric instead of isdigit
Avatar of crazy4s
crazy4s

ASKER

yes i know but it prints false for float...
Avatar of crazy4s

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
Well, you can certainly use RegEx if you want

from re import match

n.match("\d+(?:\.\d+)?")
Avatar of crazy4s

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
SOLUTION
Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
Avatar of crazy4s

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'
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 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"
...
>>>
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.
Avatar of crazy4s

ASKER

Sorry for the late... N thanks for all the suggestions!