【发布时间】:2022-06-28 01:12:11
【问题描述】:
我已经构建了以下代码:
# function to calculate bmi and return a result based on user input
def calculatebmi(weight, height):
bmivalue = weight // (height ** 2)
if bmivalue < 18.5:
print("Underweight "), print(bmivalue)
elif bmivalue >= 18.5 and bmivalue <= 24.9:
print("Healthy "), print(bmivalue)
elif bmivalue <= 25.0 and bmivalue >= 29.9:
print("Overweight "), print(bmivalue)
elif bmivalue >= 30.0:
print("Obese "), print(bmivalue)
# establish usable variables based on user input
user_weight_kg1, user_height_m1 = input("What is your weight in kilograms? "), input("What is your height in meters? ")
# convert user input to float
user_weight_kg2, user_height_m2 = float(user_weight_kg1), float(user_height_m1)
# run the function
calculatebmi(user_weight_kg2, user_height_m2)
无论出于何种原因,当我输入“75”作为体重值并输入“1.7”作为身高值时,它只是简单地返回:
What is your weight in kilograms? 75
What is your height in meters? 1.7
Process finished with exit code 0
如果我使用整数,它可以正常工作:
What is your weight in kilograms? 80
What is your height in meters? 2
Healthy
20.0
Process finished with exit code 0
我需要能够让我的用户输入一个字符串,然后将其转换为浮点数。我在这里做错了什么?感谢您的帮助!
【问题讨论】: