Game Development Reference
In-Depth Information
TypeError: bad operand type for abs(): 'str'
abs('Hello')
This error occurs when the value of an argument you pass to a function or method is of
the wrong data type. In the above example, the abs() function takes an integer or floating
point number. Passing a string for the argument results in an error.
TypeError: abs() takes exactly one argument (2 given)
abs(42, 50)
This error appears when you pass the wrong number of arguments to a function or
method, either too many or too few. The abs() function takes exactly one (and only one)
argument. In our example we pass two arguments, which results in this error.
IndexError: list index out of range
myList = ['spam', 'fizz', 'eggs']
print(myList[3])
The IndexError happens when the index you use is larger than or equal to the number of
actual items in the list. In our above example, the myList list only has 3 items in it, so the
only valid indexes to use are 0 , 1 , and 2 . The index 3 (or any other index larger than 2 ) is
larger than any of these indexes, so the code results in an IndexError.
KeyError: 'spam'
myDict = {'fizz':42, 'eggs':100}
myDict['spam']
The KeyError happens when you try to access a key in a dictionary object that does not
exist. Either the key was never added to the dictionary, was deleted previously with the
del operator, or the key you are using has a typo in it.
Search WWH ::




Custom Search