0、忘记写冒号
在 if、elif、else、for、while、class、def 语句后面忘记添加 “:”
if spam == 42 print('Hello!')
导致:SyntaxError: invalid syntax
1、误用 “=” 做等值比较
“=” 是赋值操作,而判断两个值是否相等是 “==”
if spam = 42: print('Hello!')
导致:SyntaxError: invalid syntax
2、使用错误的缩进
Python用缩进区分代码块,常见的错误用法:
print('Hello!') print('Howdy!')
导致:IndentationError: unexpected indent。同一个代码块中的每行代码都必须保持一致的缩进量
if spam == 42: print('Hello!') print('Howdy!')
导致:IndentationError: unindent does not match any outer indentation level。代码块结束之后缩进恢复到原来的位置
if spam == 42: print('Hello!')
导致:IndentationError: expected an indented block,“:” 后面要使用缩进
3、变量没有定义
if spam == 42: print('Hello!')
导致