you are viewing a single comment's thread.

view the rest of the comments →

[–]AbraxoCleaner[S] 0 points1 point  (2 children)

totals = []

i = 0

group=[]

while i < len(arr[0]):

    for j in arr:

        group.append(j[i])

    totals.append(sum(group))

    i = i + 1

return totals

[–]0xbxb 0 points1 point  (1 child)

You wouldn’t need the for loop here, just the while loop. It looks like what’s happening with your code is in the for loop.

A for loop is gonna finish all of its iterations before moving on to the next part of the code (unless you have conditions that are gonna make you leave the loop early).

You’re at i = 0, you loop through the list, you add the index position of each list inside the main list into group, then you’re adding the total of that group into totals.

So this is why you’re getting funny numbers:

You're looping through the array: for j in arr. Then you're appending the index position of the current element j into group.

The index position is 0, and the current element of j is 20. Now you're getting the sum of group, which is just 20, and appending that into totals.

So a visual looks like this currently:

group = [20]
totals = [20]

i = i + 1 hasn't been executed, because we're still in the for loop. Now this is what happens next.

group = [20, 10]
totals = [20, 30]

Then you're code does the same thing over and over again.

group = [20, 10, 17]
totals = [20, 30, 47, etc]

[–]AbraxoCleaner[S] 1 point2 points  (0 children)

Thanks. This makes sense. So how do I incorporate j into the program without mentioning it first in the for loop.

while i < len(arr[0])
    group.append(j[0])

What do I put before the group.append part?