In python, when argument data type are NaN, how to return an NaN, not an
TypeError
e.g. I defined an function which needs several input arguments, if some
keyword arguments not being assigned, typically there be an TypeError
message, but I want to change it, to output an NaN as the result, could it
be done?
def myfunc( S0, K ,r....):
if S0 = NaN or .....:
How to do it? Much appreciated.
Edited:
def myfunc(a):
return a / 2.5 + 5
print myfunc('whatever')
>python -u "bisectnewton.py"
Traceback (most recent call last):
File "bisectnewton.py", line 6, in <module>
print myfunc('whatever')
File "bisectnewton.py", line 4, in myfunc
return a / 2.5 + 5
TypeError: unsupported operand type(s) for /: 'str' and 'float'
>Exit code: 1
What I want is, the myfunc(a) only accpets an number as the input, if some
other data type like a string = 'whatever' inputed, I don't want to just
output an default error message, I want it to output something like return
'NaN' to tell others that the input should be an number.
Now I changed it to this, but still not working, btw, is none the same as
NaN? I think they're different.
def myfunc(S0):
if math.isnan(S0):
return 'NaN'
return a / 2.5 + 5
print myfunc('whatever')
>python -u "bisectnewton.py"
Traceback (most recent call last):
File "bisectnewton.py", line 8, in <module>
print myfunc('whatever')
File "bisectnewton.py", line 4, in myfunc
if math.isnan(S0):
TypeError: a float is required
>Exit code: 1
Thanks!
No comments:
Post a Comment