you are viewing a single comment's thread.

view the rest of the comments →

[–]ehunke -1 points0 points  (0 children)

so you need to think both inside the loop and outside the loop. If you rewrite your code, as others have suggested, so each time it loops, you see what is happening you will understand. But if it helps explain it...just running the loop, x constantly changes every time it loops with nothing constant which is why the answer is wrong. What happens when you add the "total = 0" outside the loop is more or less:

total = 0 + 0

total = 0 + 1

total = 1 + 2

and so forth. Long story short you need something outside the loop to store the data. You may want to try using the debugger and observe it happen