简体   繁体   English

计算用户输入的数量和这些输入的平均值

[英]Calculating how many user inputs and average of those inputs

Trying to complete python homework task, part of the program needs to calculate how many BMIs its calculated and the average value of these BMIs after the user has written 'n'.尝试完成python作业任务,部分程序需要计算它计算出多少个BMI以及用户写“n”后这些BMI的平均值。 Help would be appreciated, this is what I have now.帮助将不胜感激,这就是我现在所拥有的。

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

You need to create an empty list bmi_records before going to while loop.在进入while循环之前,您需要创建一个空列表bmi_records Then just append new BMIs to the bmi_records list.然后只需将新的 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}') 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM