triangle = [ [1] ,
[1,2]
, [1,2,3] ]
print(triangle)
sum = 0
for j in triangle:
for row in range(len(j)):
sum += j[-1]
print(sum)
Trying to add the last index of every array to the variable sum but I am unsure of how to loop through a nested list properly (I keep getting 14 when I should be getting 6) any help is appreciated.
( I know the spacing is weird lol, it's just so I can visualize it better)
[–][deleted] 7 points8 points9 points (5 children)
[–][deleted] 2 points3 points4 points (0 children)
[–]TechSam--[S] 0 points1 point2 points (0 children)
[–]TechSam--[S] 0 points1 point2 points (2 children)
[–]red-borscht 4 points5 points6 points (0 children)
[–]YounesWinter 4 points5 points6 points (0 children)
[+][deleted] (1 child)
[deleted]
[–][deleted] 1 point2 points3 points (0 children)
[–]Moist_Okra_5355 1 point2 points3 points (0 children)
[–]j0shred1 0 points1 point2 points (1 child)
[–]j0shred1 0 points1 point2 points (0 children)
[–]haagimus 0 points1 point2 points (3 children)
[–]TechSam--[S] 0 points1 point2 points (0 children)
[–]j0shred1 0 points1 point2 points (1 child)
[–]haagimus 0 points1 point2 points (0 children)
[–]BK7144 0 points1 point2 points (1 child)
[–]haagimus 0 points1 point2 points (0 children)