其他错误

AssertError

assert 语句几乎存在于每种编程语言中。当你这样做时:

assert condition

要么:

assert condition, message

它相当于:

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

断言可以包含可选消息,你可以在完成调试后禁用它们。

注意 :内置变量 debug 在正常情况下为 True,在请求优化时为 False(命令行选项 -O)。调试的分配是非法的。解释器启动时确定内置变量的值。

一个 KeyboardInterrupt

用户按下中断键时出错,通常为 Ctrl + C 或 del。

ZeroDivisionError

你试图计算未定义的 1/0。请参阅此示例以查找数字的除数:

Python 2.x <= 2.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.x >= 3.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)

它提升了 ZeroDivisionError,因为 for 循环将该值赋给 x。相反它应该是:

Python 2.x <= 2.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.x >= 3.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)