以下是我的部分代码:
students = 0
weight = 0
height = 0
while students < 5:
name = input("Please enter the name. ")
students = students + 1
while weight == 0:
try:
weight = float(input("Please enter the weight in kgs "))
if weight <= 0:
print ("Please enter a number greater than zero ")
weight = 0
continue
except ValueError:
print ("No number found, please enter a number greater than zero ")
weight = 0
continue
while height == 0:
try:
height = float(input("Please enter the height in metres "))
if height <= 0:
print ("Please enter a number greater than zero ")
height = 0
continue
except ValueError:
print ("No number found, please enter a number greater than zero ")
height = 0
continue
BMI = (weight/(height*height))
print (name, "has the BMI of", "%.2f" %BMI)
if BMI < 18.5:
print ("The student is underweight.")
elif 18.5 <= BMI <= 27:
print ("The student is a healthy weight.")
elif BMI > 27:
print ("The student is overweight.")
weight = 0
height = 0
然而,当BMI为18.5时,它表示学生体重不足,当BMI为27时,它表示学生超重,而实际上两者都应该是健康体重? e、 体重为53.456克,身高为1.7米,显示体重不足
一般来说,由于浮点数在计算机中的表示方式不同,浮点数并不精确。它们只是非常接近的近似值。因此,用浮点数测试完全相等必然会导致失败。你也许应该把你的数字四舍五入,或者不检验是否相等。你知道吗
timrau发布的链接实际上是一个很好的主题介绍,如果你有兴趣进一步探讨它。你知道吗
在我看来,这只是浮点错误,试试看
未来的问题解决策略:每当有什么事情看起来不太对劲时,打开python shell,把事情弄得乱七八糟,你可能会自己找到解决方案。你知道吗
仅浮点数近似十进制数。你的比较可以根据你乘或除的顺序或者一天中的时间来进行。。。不完全是。如果您想进行有意义的相等比较,那么可以查看python中的decimal库。你可以将精度设置为小数点后两位,突然间你的比较和计算器数学就可以了。你知道吗
相关问题 更多 >
编程相关推荐