簡體   English   中英

計算用戶輸入的數量和這些輸入的平均值

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

嘗試完成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

在進入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}') 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM