一,程序的分支结构:
- 单分支(if)
- 双分支(if,else)紧凑形式:<表达式1> if <条件> else <表达式2>
- 多分支(if,elif,else)
- 条件判断及组合(数学符号同c)保留字and,or,not(与或非)
- 程序异常处理except
#示例
try:
num = eval(input(”请输入一个整数:“))
print(num**2)
except:#可标注异常类型,仅相应该类型如except NameError
print("输入不是整数")
分支例题(BMI)
#BMI,写字板打开变乱码,懒得改了,大概是偏廋,正常,偏胖,肥胖,分国际标准和国内标准
height,weight = eval(input())
bmi = weight / pow(height,2)
print(bmi)
who,nat = "",""
if bmi< 18.5:
who,nat = "鍋忓粙","鍋忓粙"
elif 18.5 <=bmi < 24:
who,nat = "姝e父","姝e父"
elif 24 <=bmi <25:
who,nat = "鍋忚儢","鍋忚儢"
elif 25 <=bmi <28:
who,nat ="鍋忚儢","鍋忚儢"
elif 24 <=bmi <25:
who,nat ="鍋忚儢","鑲ヨ儢"
else:
who,nat = "鑲ヨ儢","鑲ヨ儢"
print("BMI鎸囨爣涓猴細鍥介檯{0},鍥藉唴{1}".format(who,nat))
原文地址:https://www.cnblogs.com/mouzaisi/p/12177840.html
时间: 2024-11-05 11:35:05