关键字
IndentationError、没有按要求缩进
异常解析
IndentationError: expected an indented block
IndentationError: 没有按要求缩进
如果程序的缩进没有规范,就会导致此问题。
Python语言是一款对缩进非常敏感的语言,给很多初学者带来了困惑,即便是很有经验的Python程序员,也可能陷入陷阱当中。最常见的情况是tab和空格的混用会导致错误,或者缩进不对,而这是用肉眼无法分别的。
实例
1 2 3 4 5 6 7 8 9 10 11 12 13 |
>>> tup1 = ('hello', 'py40', 0, 10000) >>> print(tup1[0]) hello >>> tup2 = (1) >>> tup3 = tup1+tup2 Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: can only concatenate tuple (not "int") to tuple >>> tup2 = (1,) >>> tup3 = tup1+tup2 >>> print(tup3) ('hello', 'py40', 0, 10000, 1) >>> |
解决方法
Python语言是一款对缩进非常敏感的语言,给很多初学者带来了困惑,即便是很有经验的Python程序员,也可能陷入陷阱当中。最常见的情况是tab和空格的混用会导致错误,或者缩进不对,而这是用肉眼无法分别的。
在编译时会出现这样的错IndentationError:expected an indented block说明此处需要缩进,你只要在出现错误的那一行,按空格或Tab(但不能混用)键缩进就行。
往往有的人会疑问:我根本就没缩进怎么还是错,不对,该缩进的地方就要缩进,不缩进反而会出错,,比如:
1 2 3 4 5 6 7 8 |
if xxxxxx: (空格)xxxxx 或者 def xxxxxx: (空格)xxxxx 还有 for xxxxxx: (空格)xxxxx |
一句话 有冒号的下一行往往要缩进,该缩进就缩进
未经允许不得转载:Python在线学习 » python异常-IndentationError