15,902,939 members
1.00/5 (1 vote)
See more:
```Compulsory Task 1
● In a file called sum_recursion, create:
o a function that takes a list of integers and an integer as 2
arguments. The integer will represent an index point.
o This function needs to add the sum of all the numbers in the list
up until and including the given index point by making use of
recursion and no loops.
Examples of input and output:
adding_up_to([1, 4, 5, 3, 12, 16], 4)
=> 25

What I have tried:

<pre>def sumoflist(list):
if len(list) == 1:
return list[0]
else:
return list[0] + sumoflist(list[1:])

list = [5,2,3,1]
sum = sumoflist(list)
print(sum)```
Posted
Updated 23-Jul-22 18:36pm
Richard MacCutchan 24-Jul-22 3:19am
"a function that takes a list of integers and an integer as 2
arguments. The integer will represent an index point."

Your function accepts only one argument.
```def sumoflist(list, index):
```
Thandeka Zulu 24-Jul-22 15:31pm
I do know that part. I need some assistance on how will I utilise the index inside the function.

## Solution 1

See here: Python Functions[^]
It explains the process and gives examples!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Top Experts
Last 24hrsThis month
 Richard Deeming 115 OriginalGriff 25 Kevin Marois 10 Dave Kreskowiak 10 Jo_vb.net 10
 Pete O'Hanlon 1,580 OriginalGriff 1,227 Richard Deeming 435 Richard MacCutchan 355 Dave Kreskowiak 330

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900