Python Language Other Errors


Example

AssertError

The assert statement exists in almost every programming language. When you do:

assert condition

or:

assert condition, message

It's equivalent to this:

if __debug__:
    if not condition: raise AssertionError(message)

Assertions can include an optional message, and you can disable them when you're done debugging.

Note: the built-in variable debug is True under normal circumstances, False when optimization is requested (command line option -O). Assignments to debug are illegal. The value for the built-in variable is determined when the interpreter starts.

KeyboardInterrupt

Error raised when the user presses the interrupt key, normally Ctrl + C or del.

ZeroDivisionError

You tried to calculate 1/0 which is undefined. See this example to find the divisors of a number:

Python 2.x2.02.7
div = float(raw_input("Divisors of: "))
for x in xrange(div+1): #includes the number itself and zero
    if div/x == div//x:
        print x, "is a divisor of", div
Python 3.x3.0
div = int(input("Divisors of: "))
for x in range(div+1): #includes the number itself and zero
    if div/x == div//x:
        print(x, "is a divisor of", div)

It raises ZeroDivisionError because the for loop assigns that value to x. Instead it should be:

Python 2.x2.02.7
div = float(raw_input("Divisors of: "))
for x in xrange(1,div+1): #includes the number itself but not zero
    if div/x == div//x:
        print x, "is a divisor of", div
Python 3.x3.0
div = int(input("Divisors of: "))
for x in range(1,div+1): #includes the number itself but not zero
    if div/x == div//x:
        print(x, "is a divisor of", div)