Introductory Python course - need help! Write a function that takes, as an argum
ID: 3798278 • Letter: I
Question
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.Explanation / Answer
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)
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.