Python 条件语句是使用最频繁的语句之一,语法如下:
if condition_1:
statement_block_1
elif condition_2:
statement_block_2
else:
statement_block_3
condition_1为起始条件,如果为真(True),则执行 statement_block_1 块语句。
当 condition_1 不满足时(False),则判断 condition_2 是否满足,如果为真(True),则执行 statement_block_2 块语句。
当 condition_2 也不满足时,则执行 statement_block_3 块语句。
if、elif、else均为条件控制语句的关键词。
从本章节开始,我们将结合 Blender Python API 来为大家演示语法功能,避免因为语法学习过程中的枯燥,而导致半途而废,在频道中,我们会提供更多基于Blender Python开发的算法案例,供你深入了解Python的魅力,你可以提前学习这些案例。
01 条件
01.01 基础用法
condition_1、condition_2 均可以使用由比较运算符组成的判断式作为条件。
先来看一个简单案例:
var1 = 100
if var1 > 0:
print('if 条件表达式为 True')
else:
print('if 条件表达式为 False')