4

I know similar questions have been asked and answered before like here: How do I check if a string is a number (float) in Python?

However, it does not provide the answer I'm looking for. What I'm trying to do is this:

def main():
    print "enter a number"
    choice = raw_input("> ")
    # At this point, I want to evaluate whether choice is a number.
    # I don't care if it's an int or float, I will accept either.

    # If it's a number (int or float), print "You have entered a number."
    # Else, print "That's not a number."
main()

Answers to most of the questions suggest using try..except, but this only allows me to evaluate int or floats exclusively, i.e

def is_number(s):
    try:
        float(choice)
        return True
    except ValueError:
        return False

If I use this code, int values will end up in the exception.

Other methods I've seen include str.isdigit. However, this returns True for int and false for float.

Community
  • 1
  • 1
wcj
  • 117
  • 1
  • 1
  • 7
  • 2
    `float('123')` returns `123.0` and doesn't give you `ValueError`. So your function works fine. – Remi Guan Dec 22 '15 at 22:38
  • 2
    *"If I use this code, int values will end up in the exception"* - nope. Please actually try these things before asking. – jonrsharpe Dec 22 '15 at 22:38
  • Thanks for the clarification guys. I did try that method before asking this but I couldn't get it to work. Must have made a mistake somewhere. Either way, it works now just like you guys mentioned. Thanks!! – wcj Dec 22 '15 at 22:56
  • Does this answer your question: [Checking whether a variable is an integer or not](http://stackoverflow.com/questions/3501382/checking-whether-a-variable-is-an-integer-or-not) Thomas – Thomas Dec 22 '15 at 23:22
  • Solution: https://stackoverflow.com/a/64132078/8321339 – Vishal Gupta Sep 30 '20 at 06:47

5 Answers5

4

In your case it is enough to simply check whether the input can be converted to a float in a try/except block. The conversion will be successful for any string that could have been converted to an integer as well.

timgeb
  • 76,762
  • 20
  • 123
  • 145
2

the function you used should be converting int and float values in the form of a string to a float successfully. for some reason you want to specifically want to find weather it is an int or float consider this change.

def is_int_or_float(s):
    ''' return 1 for int, 2 for float, -1 for not a number'''
    try:
        float(s)

        return 1 if s.count('.')==0 else 2
    except ValueError:
        return -1
print is_int_or_float('12')
print is_int_or_float('12.3')
print is_int_or_float('ads')

here is the result

python test.py
1
2
-1
1

You can write your own function,

def is_int_or_float(a):
    if type(a) is int or type(a) is float:
        return True
    else:
       return False

You can write even more compact program. Between please do that yourself.

Thanks!

Annapoornima Koppad
  • 1,376
  • 1
  • 17
  • 30
0

You can use the isstance() method and literal_eval

example

from ast import literal_eval

   isinstance(literal_eval('3'),int)

returns

True

or you can use json.

import json

isinstance(json.loads('3'),int)

return

True
repzero
  • 8,254
  • 2
  • 18
  • 40
0

I know this is old, but I did a simple check:

    var = 3

    try: #if it can be converted to a float, it's true
        float(var) 
    except: # it it can't be converted to a float, it's false
        do stuff with the var 
Mike
  • 2,531
  • 6
  • 26
  • 34