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.

Respuesta :

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

Explanation:

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

Image of sample list used is attached.

Ver imagen ibnahmadbello