Hi, I am stuck on this one. It makes no sense to me. I did attempt, but I'm having no luck. I think it comes down to knowing how to get the first n numbers of the list. Below is my code so far. If I am just not understanding what this is asking, please let me know and guide me a little bit here. I know I am fairly close to getting the answer on my own.
Given a list and an integer n, return the sum of the first n numbers in the list.
Examples
sum_first_n_nums([1, 3, 2], 2) ➞ 4
sum_first_n_nums([4, 2, 5, 7], 4) ➞ 18
sum_first_n_nums([3, 6, 2], 0) ➞ 0
Notes
If n is larger than the length of the list, return the sum of the whole list.
Given a list and an integer n, return the sum of the first n numbers in the list.
Examples
sum_first_n_nums([1, 3, 2], 2) ➞ 4
sum_first_n_nums([4, 2, 5, 7], 4) ➞ 18
sum_first_n_nums([3, 6, 2], 0) ➞ 0
Notes
If n is larger than the length of the list, return the sum of the whole list.
def sum_first_n_nums(lst, n): if n <= len(lst): return n * (n + 1) / 2 elif n > len(lst): return sum(lst)
def sum_first_n_nums(lst, n): if n <= len(lst): return lst[n:] elif n > len(lst): return sum(lst)