NumPy - using isnan (x)

I am trying to use numpy to check if user input is numeric, I tried using:

from numpy import *

a = input("\n\nInsert A: ")

if isnan(a) == True:
    print 'Not a number...'
else:
    print "Yep,that a number"

It’s own on it, and it works fine, however, when I insert it into a function, such as in this case:

from import numpy *

def test_this(a):

    if isnan(a) == True:
        print '\n\nThis is not an accepted type of input for A\n\n'
        raise ValueError
    else:
        print "Yep,that a number"

a = input("\n\nInsert A: ")

test_this(a)

Then I get a NotImplementationError message that it is not implemented for this type, can someone explain how this does not work?

Any help would be greatly appreciated and thanks again.

+5
source share
4 answers

" " "NaN" - IEEE-754. numpy.isnan() math.isnan() , ( "NaN" ). , , TypeError.

​​ , , input(). raw_input(), try:, float , .

:

def input_float(prompt):
    while True:
        s = raw_input(prompt)
        try:
            return float(s)
        except ValueError:
            print "Please enter a valid floating point number."

@J.F. ,

input() eval(raw_input(prompt)), , , .

, , raw_input , eval , , .

+11

Python - float .

, NaN - , , Not a Number.

def check_if_numeric(a):
   try:
       float(a)
   except ValueError:
       return False
   return True
+2
a = raw_input("\n\nInsert A: ")

try: f = float(a)
except ValueError:
     print "%r is not a number" % (a,)
else:
     print "%r is a number" % (a,)
0

:

a = input("\n\nInsert A: ")
num_types = ("int", "float", "long", "complex")

if type(a).__name__ in num_types
    print "Yep,that a number"       
else:
    print '\n\nThis is not an accepted type of input for A\n\n'
    raise ValueError
0

All Articles