# calculate the sum of a list

numbers = [2, 4, 6, 8, 10]
total = 0
# TO DO: add each number to total using a loop
# for nun in ____:
# total = ___
# print (“ Sum is:”, total)

1 answer

You can complete the code to calculate the sum of your list using a loop. Here’s how to do it:

numbers = [2, 4, 6, 8, 10]
total = 0

# Add each number to total using a loop
for num in numbers:
    total += num

print("Sum is:", total)

In this code:

  • The for loop iterates over each number in the numbers list.
  • The total += num line adds the value of num to total for each iteration.
  • Finally, it prints the total sum of the numbers in the list.

When you run this code, it will output:

Sum is: 30