try:
f = open('blah', 'r')
except IOError, e:
print 'could not open file:', e
print float(12345)
print float('12345')
print float('123.45e67')
def safe_float(obj):
try:
retval = float(obj)
except ValueError:
retval = 'could not convert non-number to float'
return retval
print safe_float('12.34')
print safe_float('bad input')
print safe_float({'a': 'Dict'})
|