35.4k views
1 vote
Write a function sum them() that sums a set of integers, each of which is the square or cube of a provided integer. Specifically, given an integer n from a list of values, add n2 to the total if the number’s index in nums is even; otherwise, the index is odd, so add n3 to the total.

User Peter Lur
by
6.0k points

1 Answer

2 votes

Answer:

# sum_them function is defined

def sum_them():

# A sample of a given list is used

given_list = [1, 2, 3, 4, 5, 6, 6, 9, 10]

# The total sum of the element is initialized to 0

total = 0

# for-loop to goes through the list

for element in given_list:

# if the index of element is even

# element to power 2 is added to total

if(given_list.index(element) % 2 == 0):

total += (element ** 2)

else:

# else if index of element is odd

# element to power 3 is added to total

total += (element ** 3)

# The total sum of the element is displayed

print("The total is: ", total)

# The function is called to display the sum

sum_them()

Step-by-step explanation:

The function is written in Python and it is well commented.

Image of sample list used is attached.

Write a function sum them() that sums a set of integers, each of which is the square-example-1
User Mohinder Singh
by
5.5k points