how to check if a value is a floating point or not

Sturla Molden sturla.molden at gmail.com
Thu Jun 19 09:46:52 EDT 2014


<nicholascannon1 at gmail.com> wrote:
> I am making a calculator and i need it to support floating point values
> but i am using the function isnumeric to check if the user has entered an
> int value. I need the same for floating point types so i could implement
> an or in the if statement that checks the values the user has entered and
> allow it to check and use floating points. If you need the source code i
> am happy to give it to you. Thank you for your help

It's better to ask forgiveness than ask permission...

You don't have to check anything. If the user enters something that cannot
be coverted to a float, the function float() will raise an exception:

try: 
    x = float(value)
except ValueError:
    # not a float
    pass


Sturla




More information about the Python-list mailing list