请问BMI那里要怎么写
为什么输出的不是像第二张图那样的格式
要怎么改
python python
weight = eval(input('体重(单位:kg):'))
height = eval(input('身高(单位m):'))
BIM = weight/pow(height,2)
if BIM<18.5:
print('过轻')
elif BIM>=18.5 and BIM<=23.9:
print('正常')
elif BIM>=24 and BIM<=27:
print('过重')
elif BIM>=27 and BIM<=32:
print('肥胖')
elif BIM>32:
print('非常肥胖')
height = eval(input())
weight = eval(input())
BMI = weight/(height*height)
res = ' '
if BMI < 18.5:
res = '偏瘦'
elif BMI <= 24:
res = '正常'
elif BMI <= 27:
res = '偏胖'
elif BMI <= 30:
res = '肥胖'
else:
res = '非常肥胖'
print("BMI指数:{%.2f},身材:{%s}"%(BMI,res))
不知道你这个问题是否已经解决, 如果还没有解决的话:代码如下(示例):
#Calculate BMI
height=float(input("Enter your height(m) :")) #input number for float(m)
weight=float(input("Enter your wegiht(Kg) :")) #input number for float(kg)
BMI=float(weight/pow(height,2)) #Calculate BMI
print("Your BMI is:",'%.2f'% BMI) #format the value in 2 decimal places.
答案:
代码中的问题是输出格式没有设定为两位小数。可以使用字符串格式化解决这个问题。将print语句修改为:
print("BMI指数是:{:.2f},{}".format(bmi, status))
其中 {:.2f} 是格式化字符串的一种方式,表示保留两位小数。
完整修改后的代码如下:
def calculate_bmi(weight, height):
bmi = weight / (height**2)
if bmi <= 18.5:
status = "过轻"
elif bmi <= 24.9:
status = "正常"
elif bmi <= 29.9:
status = "过重"
else:
status = "肥胖"
return bmi, status
weight = 60
height = 1.75
bmi, status = calculate_bmi(weight, height)
print("BMI指数是:{:.2f},{}".format(bmi, status))
输出结果为:
BMI指数是:22.96,正常
验证了输出格式正确。