[Tutor] Dividing a float derived from a string
Peter Otten
__peter__ at web.de
Fri Nov 21 15:15:21 CET 2014
Alan Gauld wrote:
> But that's considered bad practice, it's better to put the
> valid errors only in the except line like this:
>
> try:
> print float(input)*12
> except TypeError, ValueError:
> print False
Careful, you need parens around the tuple of errors, otherwise this catches
only TypeErrors and assigns the TypeError instance to ValueError:
>>> input = None
>>> try: print float(input) * 12
... except TypeError, ValueError: print False
...
False
>>> ValueError
TypeError('float() argument must be a string or a number',)
This is a nasty gotcha best avoided by using Python 3.
More information about the Tutor
mailing list