计算多少用户输入并将这些输入平均

时间:2019-12-08 23:53:00

标签: python average

试图完成python家庭作业任务,该程序的一部分需要计算其计算出的BMI数以及用户写入“ n”后这些BMI的平均值。帮助将不胜感激,这就是我现在所拥有的。

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

1 个答案:

答案 0 :(得分:0)

进入bmi_records循环之前,您需要创建一个空列表while。然后只需将新的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}')