On 5/18/19 2:20 AM, marcus lütolf wrote: > Dear experts > > in learning the principles of Python I came across scope in the > control structure's section. > There I read the notion that variables createted inside a > control structute can't be seen or accessed from outside that > structure, Python would raise a Name Error. > > However in for loops - also control structures - like in this > simple example: > > for i in range(1,11): > sum = 0 > sum += i > print(sum) > > the print function "returns" the last number of range as > value of the variable sum created inside this for loop. > It should have raised a Name Error instead. > Could somebody please explain this discrepancy?
in addition to Alan's comment, in your loop you are resetting sum to zero each time through, which is why at the end of the loop sum is the same as the finishing value of i, not the sum of the values... _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor