0.这次实现的主要功能改变是,用户只需要输入一行信息,通过字符串进行分割,最后再格式化输出
1.程序实现
"""
计算BMR,人体代谢率指数
一行输入所有结果,然后带单位输出
格式化输出+字符串分割
"""
def cal_BMR(gender, height, weight, age):
"""
计算BMR函数
BMR:基础代谢率
:return:无
"""
bmr = -1
if gender == '男':
bmr = weight*13.7+height*5.0-age*6.8+6
elif gender == '女':
bmr = weight*9.6+height*1.8-age*4.7+655
return bmr
if __name__ == '__main__':
continue_cal = 'y'
while(continue_cal=='y'):
str_all_info = input("请输入以下信息(并以空格分隔):性别(男/女)、体重(kg)、身高(cm)、年龄:")
# 将字符串分割
list_info = str_all_info.split(" ")
gender = list_info[0]
weight = eval(list_info[1])
height = eval(list_info[2])
age = eval(list_info[3])
bmr = cal_BMR(gender, height, weight, age)
# 格式化输出
print("您的性别为:{},体重为:{}kg,身高为:{}cm,年龄为:{}岁".format(gender, weight, height, age))
print("基础代谢率:{}大卡".format(bmr))
continue_cal = input('是否继续,继续则y,否则n')
2.结果
Y:\Python\Anaconda\python.exe Y:/PythonWorkspace/lect04/CalBMR3.py
请输入以下信息(并以空格分隔):性别(男/女)、体重(kg)、身高(cm)、年龄:男 54.5 168 23
您的性别为:男,体重为:54.5kg,身高为:168cm,年龄为:23岁
基础代谢率:1436.25大卡
是否继续,继续则y,否则nn
Process finished with exit code 0
3.下一次加入异常处理
未完待续眼