簡體   English   中英

如何計算列表中數字的總和?

[英]How to calculate sum of numbers in a list?

我正在嘗試編寫一個程序,其中用戶輸入 8 歲的客人。 每位客人的年齡都不同。 如何將每項費用加在一起得出每位客人的總費用? 這就是我寫的。

def main():
    guest1= int(input("Enter the age of guest 1(in years): "))
    guest2 = int(input("Enter the age of guest 2(in years): "))
    guest3 = int(input("Enter the age of guest 3(in years): "))
    guest4 = int(input("Enter the age of guest 4(in years): "))
    guest5 = int(input("Enter the age of guest 5(in years): "))
    guest6 = int(input("Enter the age of guest 6(in years): "))
    guest7 = int(input("Enter the age of guest 7(in years): "))
    guest8 = int(input("Enter the age of guest 8(in years): "))
    ages = [guest1, guest2, guest3, guest4, guest5, guest6, guest7, guest8]
    getCost(ages)


def getCost(ages):
    for age in ages:
        if age <=2:
            cost = 0
            print(cost)
        elif age <= 3>12:
            cost = 14
            print(cost)
        elif age >= 65:
            cost = 18
            print(cost)
        else:
            cost = 23
            print(cost)
        
        
main()

您可以通過在循環中調用input()將所有年齡預先放入一個列表中,而不是復制和粘貼八次:

ages = [int(input(f"Enter the age of guest {i}(in years): ")) for i in range(1, 9)]

我建議讓您的getCost function 只返回一個年齡的成本(提示:通過按升序排列年齡范圍,並根據其上限一次消除一個來簡化此操作):

def getCost(age):
    if age < 3:
        return 0
    elif age < 12:
        return 14
    elif age < 65:
        return 23
    else:
        return 18

這使得getCost的工作變得更加容易——然后在你得到ages之后,你可以通過為每個age調用getCost(age)並獲取結果的sum來計算總成本:

print(sum(getCost(age) for age in ages))

將邏輯拆分成這樣的部分可以很容易地用成本做其他事情; 例如,您可以通過在sum之前join成本來顯示總計是如何計算的:

print(
    " + ".join(str(getCost(age)) for age in ages),
    "=",
    sum(getCost(age) for age in ages)
)
Enter the age of guest 1(in years): 2
Enter the age of guest 2(in years): 9
Enter the age of guest 3(in years): 13
Enter the age of guest 4(in years): 27
Enter the age of guest 5(in years): 44
Enter the age of guest 6(in years): 52
Enter the age of guest 7(in years): 66
Enter the age of guest 8(in years): 81
0 + 14 + 23 + 23 + 23 + 23 + 18 + 18 = 142

您可以從定義一個totalCost變量開始,作為一個運行總計,您可以在計算每個客戶的成本后繼續遞增。 例如:

def getCost(ages):
    totalCost = 0
    for age in ages:
        if age <=2:
            cost = 0
        elif age >= 3 and age < 12:
            cost = 14
        elif age >= 65:
            cost = 18
        else:
            cost = 23
        totalCost += cost
    return totalCost

請注意,我假設age <= 3>12是指 3 到 11 歲之間的年齡,包括 3 到 11 歲。

您似乎使用 function getCost來計算每個成本。 添加一個變量來總結所有值的一點點修改將幫助你得到你想要的。

def sumCost(ages):
    total = 0
    for age in ages:
        if age <=2:
            cost = 0
        elif age <= 3>12: # your original code may have some problems here. I just keep it.
            cost = 14
        elif age >= 65:
            cost = 18
        else:
            cost = 23
        total  = total + cost
    return total

或者您可以構建成本列表,然后使用sum

def sumCost(ages):
    costs = []
    for age in ages:
        if age <=2:
            cost = 0
        elif age <= 3>12: # your original code may have some problems here. I just keep it.
            cost = 14
        elif age >= 65:
            cost = 18
        else:
            cost = 23
        costs.append(cost)
    total = sum(costs)
    return total

暫無
暫無

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

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