简体   繁体   English

如何计算列表中数字的总和?

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

I am trying to write a program in which the user enters the age of 8 guests.我正在尝试编写一个程序,其中用户输入 8 岁的客人。 Each guest's age is priced differently.每位客人的年龄都不同。 How can I add each cost together to come up with the total cost for each guest?如何将每项费用加在一起得出每位客人的总费用? This is what I have written.这就是我写的。

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()

You can put all the ages into a list up front by calling input() in a loop instead of copying and pasting it eight times:您可以通过在循环中调用input()将所有年龄预先放入一个列表中,而不是复制和粘贴八次:

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

I'd suggest having your getCost function just return the cost for a single age (hint: make this simpler by arranging the age brackets in ascending order, and eliminating them one at a time according to their upper bound):我建议让您的getCost function 只返回一个年龄的成本(提示:通过按升序排列年龄范围,并根据其上限一次消除一个来简化此操作):

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

This makes the job of getCost much easier -- then after you've gotten ages you can compute the total cost just by calling getCost(age) for each age and taking the sum of the result:这使得getCost的工作变得更加容易——然后在你得到ages之后,你可以通过为每个age调用getCost(age)并获取结果的sum来计算总成本:

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

Splitting the logic into parts like this makes it easy to do other things with the costs;将逻辑拆分成这样的部分可以很容易地用成本做其他事情; for example, you could show how the total was computed by join ing the costs before you sum them:例如,您可以通过在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

You can start by defining a totalCost variable, as a running total that you can keep incrementing after you calculate the cost for each customer.您可以从定义一个totalCost变量开始,作为一个运行总计,您可以在计算每个客户的成本后继续递增。 For instance:例如:

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

Do note that I've assumed age <= 3>12 refers to ages between 3 and 11, inclusive.请注意,我假设age <= 3>12是指 3 到 11 岁之间的年龄,包括 3 到 11 岁。

It seems that you use function getCost to calculate each cost.您似乎使用 function getCost来计算每个成本。 A little bit modification of adding a variable to sum up all values will help you get what you want.添加一个变量来总结所有值的一点点修改将帮助你得到你想要的。

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

Or you can construct a list of cost then use sum .或者您可以构建成本列表,然后使用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