if 语句的三种形式:
第一种:
if condition:
语句1
语句2
......
举例:
if True:
print("T")
运行结果:

第二种:
if condition:
语句1
语句2
......
else:
语句1
语句2
......
举例:
price = 6300
money = 5000
if money >= price:
print("购买英雄成功")
else:
print("金币不足,请充值!!!")
运行结果:

第三种:
if condition1:
语句1
elif condition2:
语句2
elif condition3:
语句3
......
elif conditionN:
语句N
else:(可选)
语句N+1
举例:
score = 90
if score <= 60:
print("D")
elif score <= 70:
print("C")
elif score <= 85:
print("B")
else:
print("A")
运行结果:

本文详细介绍了Python编程中条件控制语句if、if-else和if-elif-else的使用方法,通过实例展示了如何根据条件执行不同的代码块。这些语句在程序逻辑中起到关键作用,帮助开发者实现条件判断和流程控制。
3495

被折叠的 条评论
为什么被折叠?



