IndentationError:expected an indented block

条件判断通过 if 语句实现

Tab 键缩进  四个空格

根据 Python 的缩进规则,如果 if 语句判断是 True,就把缩进的两行 print语句执行了,否则,什么也不做。

age=3
if age>18:
	print("your are a adult")
else:
	print("your are a kid")
>>>your are a kid

需要严格遵循缩进,否则会报错

当print没有按要求缩进时,系统会报出如下的错误提示

IndentationError:expected an indented block


elif 是 else if 的缩写,完全可以有多个 elif,所以 if 语句的完整形式


if <条件判断 1>:

          <执行 1>

elif <条件判断 2>:

          <执行 2>

elif <条件判断 3>:

           <执行 3>

else:

           <执行 4>

if 语句执行有个特点,它是从上往下判断,如果在某个判断上是 True,把该判断对应的语句执行后,就忽略掉剩下的 elif 和 else


if 判断条件还可以简写,比如写:

if x :

print('True')

只要 x 是非零数值、非空字符串、非空 list 等,就判断为 True,否则为False。


猜你喜欢

转载自blog.csdn.net/weixin_39104294/article/details/78493327