day2019.5.16
根据判断条件结果而选择不同向前路径的运行方式
if <条件>:
<语句块> ##true or false
举例
guess = eval(input())
if guess == 99:
print("猜对了)
根据判断条件结果而选择不同向前路径的运行方式
if <条件>:
<语句1>
else :
<语句2>
guess = eval(input)
if guess == 99:
print("猜对了")
else :
print("猜错了")
<表达式1> if <条件> else <表达式2>
guess = eval(input())
print("猜{}了".format("对" if guess == 99 else "错"))
if <条件>:
<语句1>
elif :
<语句2>
else:
<语句3>
举例
score = eval(input())
if score <= 60:
grade = "D"
elif score <= 70:
grade = "C"
elif score <= 80:
grade = "B"
elif score <= 90:
grade = "A"
else:
grade = "不及格"
print("输入成绩属于级别{}".format(grade))
guess = eval(input())
if guess > 99 or guess < 99: # =99就是非><99
print("猜错了")
else:
print("猜对了")
num = eval(input("请输入一个整数:"))
print(num**2) ##若用户没有输入整数呢?
##改进
try :
num = eval(input("请输入一个整数:"))
print(num**2)
except NameError:
print("输入不是整数")
try :
<语句1>
except :
<语句2>
try :
<语句1>
excep :
<语句2>
else : ##语句3在不发生异常时执行
<语句3>
finally : ##语句4一定执行
<语句4>
BMI:对身体质量的刻画
BMI:Body Mass Index,体重(kg)/身高2(m2)
height, weight = eval(input('请输入身高(米)和体重(公斤)[逗号隔开]:'))
bmi = weight / pow(height, 2)
print("BMI值为:{:.2f}".format(bmi))
who, nat = "",""
if bmi < 18.5:
who, nat= '偏瘦', "偏瘦"
elif 18.5 <= bmi < 24:
who, nat = '正常',"正常"
elif 24 <= bmi <25:
who, nat = '正常', "偏胖"
elif 25 <= bmi <28:
who, nat = "正常", "偏胖"
elif 28 <= bmi < 30:
who, nat = '偏胖', "偏胖"
print("'BMI指标为:国际'{0}'".format(who))