计算有多少用户输入和这些输入的平均值
Calculating how many user inputs and average of those inputs
试图完成python家庭作业任务,部分程序需要计算其计算的BMI数以及用户写完后这些BMI的平均值'n'。帮助将不胜感激,这就是我现在所拥有的。
y = True
while y:
weight = float(input("Enter weight in kg:"))
height = float(input("Enter height in m:"))
bmi = weight/(height**2)
if bmi <18:
print("Your BMI is", bmi, "\nIt indicates you are underweight")
elif bmi >=18 and bmi <=25:
print("Your BMI is", bmi, "\nIt indicates you are within normal bounds")
elif bmi >25:
print("Your BMI is", bmi, "\nIt indicates you are overweight")
again = str(input("Another BMI? y/n:"))
if again == "n":
y = False
在进入 while
循环之前,您需要创建一个空列表 bmi_records
。然后只需将新的 BMI 添加到 bmi_records
列表即可。
bmi_records = []
while ....
bmi = ...
bmi_records.append(bmi)
....
print(f'Average of {len(bmi_records)} BMIs entered: {sum(bmi_records)/len(bmi_records):.1f}')
试图完成python家庭作业任务,部分程序需要计算其计算的BMI数以及用户写完后这些BMI的平均值'n'。帮助将不胜感激,这就是我现在所拥有的。
y = True
while y:
weight = float(input("Enter weight in kg:"))
height = float(input("Enter height in m:"))
bmi = weight/(height**2)
if bmi <18:
print("Your BMI is", bmi, "\nIt indicates you are underweight")
elif bmi >=18 and bmi <=25:
print("Your BMI is", bmi, "\nIt indicates you are within normal bounds")
elif bmi >25:
print("Your BMI is", bmi, "\nIt indicates you are overweight")
again = str(input("Another BMI? y/n:"))
if again == "n":
y = False
在进入 while
循环之前,您需要创建一个空列表 bmi_records
。然后只需将新的 BMI 添加到 bmi_records
列表即可。
bmi_records = []
while ....
bmi = ...
bmi_records.append(bmi)
....
print(f'Average of {len(bmi_records)} BMIs entered: {sum(bmi_records)/len(bmi_records):.1f}')