[Background info: I am currently completing a course on Python programming from the MIT edx site, and am working on the section on while loops.]
The question I have been struggling with is as follows: "Write a while loop that sums the values 1 through end, inclusive. end is a variable that we define for you."
When I tried answering the question, I put:
while end != 0:
total = 0 + end
end = end-1
print total
The return result for any value I put in for 'end' was 1, which obviously is incorrect.
However, when I tried again, I defined 'total' outside of the loop, and put:
total = 0
while end != 0:
total = total + end
end = end-1
print total
This works!
My question is: why does the first code that I put in not work? What is the significance of defining 'total' outside of the loop?