1. 条件结构 Condition Structure 程序框图:
2. 代码演示 Code Demo:
3. 上述代码修改升级:
str_tip="Simple Condition Structure"
print(str_tip)
print("-"*len(str_tip))
print("A")
print("B")
C=input("Please Input String:")
if C=="D1":
print("D1")
elif C=="D2":
print("D2")
else:
print("C条件都不满足")
print("E")
4. 按照上述思路,新建下方代码:
str_tip="欢迎进行分数等级测试!"
print(str_tip)
print("-"*len(str_tip)*2)
strscore=input("请输入你的分数:\n")
fscore=float(strscore)
print("开始分析...")
if fscore>=90:
print("优秀")
elif fscore>=80:
print("良好")
elif fscore>=70:
print("中等")
elif fscore>=60:
print("及格")
else:
print("不及格")
print("-"*len(str_tip)*2)
print("测试结束。")
5. 修改上述代码,通过 延时函数 模拟一个思考分析过程(此处模拟代码以后会升级,模拟毕竟是假的):
import time
str_tip="欢迎进行分数等级测试!"
print(str_tip)
print("-"*len(str_tip)*2)
strscore=input("请输入你的分数:\n")
fscore=float(strscore)
str_sleep="开始分析..."
print(str_sleep)
time.sleep(2)
if fscore>=90:
print("优秀")
elif fscore>=80:
print("良好")
elif fscore>=70:
print("中等")
elif fscore>=60:
print("及格")
else:
print("不及格")
print("-"*len(str_tip)*2)
print("测试结束。")
6.除了上述的普通的条件结构格式,还有下面几种特殊的条件结构:
a,b,c = 1,2,3
# 条件表达式
c = a if a>b else b
# 先执行中间的if,如果返回True,就是左边,False是右边。
print(c)
# 二维列表条件结构
c = [b,a][a>b]
#实际是[b,a][False],因为False被转换为0,所以是[1,2][0],也就是[1]
# False返回第一个,True 返回第二个。
print(c)