现在的位置: 首页 > 综合 > 正文

17个新手常见Python运行时错误

2014年02月26日 ⁄ 综合 ⁄ 共 597字 ⁄ 字号 评论关闭

原文链接 / OSChina.NET原创翻译


当初学 Python 时,想要弄懂 Python 的错误信息的含义可能有点复杂。这里列出了常见的的一些让你程序 crash 的运行时错误。

1)忘记在 if , elif else for while class ,def 声明末尾添加
:(导致 “SyntaxError :invalid syntax”)

该错误将发生在类似如下代码中:

1 if spam == 42
2     print('Hello!')

2)使用 = 而不是 ==(导致“SyntaxError: invalid syntax”)

 = 是赋值操作符而 == 是等于比较操作。该错误发生在如下代码中:

1 if spam = 42:
2     print('Hello!')

3)错误的使用缩进量。(导致“IndentationError:unexpected indent”、“IndentationError:unindent does not match any outer
indetation level
”以及“IndentationError:expected an indented block”)

记住缩进增加只用在以:结束的语句之后,而之后必须恢复到之前的缩进格式。该错误发生在如下代码中:

01 print('Hello!')
02     print('Howdy!')
03
04 或者:
05
06 if spam == 42:
07

抱歉!评论已关闭.