scalar vs array and program control

Chris Angelico rosuav at gmail.com
Sat Jul 25 13:34:11 CEST 2015


On Sat, Jul 25, 2015 at 9:01 PM, Laura Creighton <lac at openend.se> wrote:
> How did I know to look for ValueErrors?
>
>>>> int("1.2")
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
>   ValueError: invalid literal for int() with base 10: '1.2'
>
> Cause that is what Python gives you.  If it had given you a TypeError
> instead -- which is what I expected it to do, but so it goes -- I
> would have said except TypeError.

The difference between "12", which is valid input for int(), and
"1.2", which isn't, is not the type of the object (both are str), but
their values. That's why the exception is a ValueError. If you pass it
a completely invalid *type*, then you get this:

>>> int([])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: int() argument must be a string, a bytes-like object or a
number, not 'list'

ChrisA


More information about the Python-list mailing list