Home > other >  How to calculate sum of numbers in a list?
How to calculate sum of numbers in a list?

Time:07-26

I am trying to write a program in which the user enters the age of 8 guests. 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()

CodePudding user response:

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:

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

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:

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 joining the costs before you sum them:

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

CodePudding user response:

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. 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.

CodePudding user response:

It seems that you use function getCost to calculate each cost. 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.

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
  • Related