摘要:今天学习了python中常用的几个循环语句,判断条件的使用,并且完成了简单demo来加深对这些知识的巩固。
一、问题:天天向上
A:一年365天,每天进步1% B:工作5天休息2天,休息日下降1% 问:B的日工作量达到多少,才能和A一样。
def dayUP(df):#根据df参数计算日工作量的函数
dayup = 1
for i in range(365):
if i % 7 in [6, 0]:
dayup = dayup * (1 - 0.01)
else:
dayup = dayup * (1 + df)
return dayup
def total():#每天进步1%
dayup = 1
dayfactor = 0.01
dayup = pow(dayup+dayfactor, 365)
return dayup
dayfactor = 0.01
while dayUP(dayfactor) < total(): #判断条件
dayfactor += 0.001
print("工作日的努力参数是:{:.3f} ".format(dayfactor))
二、问题:计算身体质量指标BMI
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 = "偏胖", "肥胖"
else:
who, nat = "肥胖", "肥胖"
print("BMI 指标为:国际'{0}', 国内'{1}'".format(who, nat))