[Tutor] Value Error

Sander Sweers sander.sweers at gmail.com
Wed Jun 12 23:06:57 CEST 2013


On 06/12/2013 10:49 PM, Jim Mooney wrote:
> Raised when a built-in operation or function receives an argument that has
> the right type but an inappropriate value, and the situation is not
> described by a more precise exception such as
> IndexError<http://docs.python.org/2/library/exceptions.html#exceptions.IndexError>

You get this when the function gets the right object but the value of
that object is not correct. For example int() will attempt to create an
integer object from a string object. However if that string is not a
number represented as a sting you run into a ValueError.

int('test')

Traceback (most recent call last):
  File "<pyshell#0>", line 1, in <module>
    int('test')
ValueError: invalid literal for int() with base 10: 'test'

When we give it the string representation of 123 int() will convert the
string to an integer.

int('123')
123

~Sander


More information about the Tutor mailing list