Showing posts with label Python float. Show all posts
Showing posts with label Python float. Show all posts

Monday, April 13, 2009

Python float input validation

If you put 'nan' in as a string, a cast to float will not raise an exception.

I am not sure this is a good design, but the counterintuitive workaround is this.

To test if a Python float is not a number, see if it is equal to itself!
try:
....val = float(raw_input("Air speed of an unladen swallow: "))
except:
....print "foo"
if val != val
....print "foo also!"