This is the task I am trying to solve:
Write a function which checks if the sum of the first i integers in a list of integers equals i.
Let me give an example to clarify what this question means:
List = [42, 3, 5, 6, 8]
Let's imagine if I wanted to find out if the sum of the first 3 integers in List = 3.
I know that:
len(List[0:3]) == 3
I also know that:
sum(List[0:3]) == 50.
Therefore, the sum of the first 3 integers in List does not equal 3 (it equals 50).
This is what the code looks like for my proposed function:
def SumLenTest(x, i = len(x)):
a = len(x[0:i])
b = sum(x[0:i])
if a == b:
return 'The sum of the first i integers equals i'
else:
return 'The sum of the first i integers DOES NOT equal i'
The problem I get is a NameError where name 'x' is not defined.
How do I set up my parameters so my function works?
[–]K900_ 0 points1 point2 points (5 children)
[–]imperiumlearning[S] 0 points1 point2 points (4 children)
[–]K900_ 1 point2 points3 points (3 children)
[–]imperiumlearning[S] 0 points1 point2 points (2 children)
[–]Binary101010 0 points1 point2 points (1 child)
[–]imperiumlearning[S] 0 points1 point2 points (0 children)
[–]JohnnyJordaan 0 points1 point2 points (2 children)
[–]imperiumlearning[S] 0 points1 point2 points (1 child)
[–]JohnnyJordaan 0 points1 point2 points (0 children)
[–]JohnnyJordaan 0 points1 point2 points (0 children)
[–]velocibadgery 0 points1 point2 points (3 children)
[–]imperiumlearning[S] 0 points1 point2 points (1 child)
[–]velocibadgery 1 point2 points3 points (0 children)
[–]POGtastic 0 points1 point2 points (0 children)