Python IndentationError 异常
定义和用法
当代码缩进缺失或错误时,会引发 IndentationError
异常。
在同一个代码块中必须使用相同数量的空格进行缩进,否则会出现 IndentationError
。
实例
例子 1
如果缺少缩进将会引发 IndentationError
:
if 5 > 2: print("Five is greater than two!") # 缺少缩进
例子 2:异常处理
如果缩进不正确将会引发 IndentationError
:
if 5 > 2: print("Five is greater than two!") # 2个空格 print("Makes sence!") # 3个空格(不一致)