• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 312
  • Last Modified:

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
0
crazy4s
Asked:
crazy4s
2 Solutions
 
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
 
farzanjCommented:
For your case, you may reverse your logic and use isalpha() member function.
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
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
 
gelonidaCommented:
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

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now