I'm learning Python and have a hard time understanding what's wrong with my logic in this nested loop.
numbers = [4, 3, 1, 3, 5]
sum = 0
while sum < 10:
for n in numbers:
sum += n
print(sum)
While sum is less than 10, iterate through the next object in a list and add it to the sum. Thus, it's supposed to print 11 (4+3+1+3, then it stops), but instead it prints 16. I have tried changing the order of loops:
for n in numbers:
while sum < 10:
sum += n
But then it prints 12, which further confuses me.
Can anybody please help? Thank you!
print()statements inside your loops to actually trace the execution.built-insumas variable name.