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
crazy4sAsked:
Who is Participating?
 
farzanjConnect With a Mentor Commented:
For your case, you may reverse your logic and use isalpha() member function.
0
 
farzanjCommented:
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();
0
 
farzanjCommented:
Here is a link to the string doc you should really look at
http://docs.python.org/library/stdtypes.html
0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

 
farzanjCommented:
Or you can use isnumeric instead of isdigit
0
 
crazy4sAuthor Commented:
yes i know but it prints false for float...
0
 
crazy4sAuthor Commented:
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
0
 
farzanjCommented:
Well, you can certainly use RegEx if you want

from re import match

n.match("\d+(?:\.\d+)?")
0
 
crazy4sAuthor Commented:
can you explain this more in details... and i couldn't find match attribute in my python ver 3.2.2... thanks:)
0
 
farzanjCommented:
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
0
 
gelonidaConnect With a Mentor Commented:
You can also make it differently.

Just try to convert it to a float and catch the exception if it fails.
try:
    n1 =  float(n1)
except ValueError:
    print "cannot convert  %s  to float %s" % n1

Open in new window

0
 
crazy4sAuthor Commented:
>>>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'
0
 
farzanjCommented:
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'
0
 
farzanjCommented:
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"
...
>>>
0
 
farzanjCommented:
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.
0
 
crazy4sAuthor Commented:
Sorry for the late... N thanks for all the suggestions!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.