Introductory Python course need help Write a function that

Introductory Python course - need help!

Write a function that takes, as an argument, a list, and an integer, n. If the integer is negative, the function should return a list containing two lists: the original list AND a list containing | n | zeros (recall that | n | means the absolute value of n). If the integer is 0, it should return the last element in the list. Otherwise, it should return the sum of the elements in the list. Name this function finalFunction(myList, n). For example, >>>finalFunction ([l, 2, 3], -1) should return the list [1, 2, 3], [0]]. As another example, >>>finalFunction ([l, 2, 3], 0) should return the value 3. As a final example, >>>finalFunction([l, 2, 3], 5) should return the value 6.

Solution

def finalFunction(myList, n):
   if n < 0:
       return [myList, [0] * -n]
   elif n == 0:
       return myList[-1]
   else:
       return sum(myList)

print finalFunction([1, 2, 3], -1)
print finalFunction([1, 2, 3], 0)
print finalFunction([1, 2, 3], 5)

Introductory Python course - need help! Write a function that takes, as an argument, a list, and an integer, n. If the integer is negative, the function should

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site