【格码顶端编程】5.Python 条件结构 Condition Structure

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)

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

gCodeTop 格码拓普 老师

您的鼓励.我的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值