Mar-02-2019, 02:01 PM
(This post was last modified: Mar-02-2019, 02:02 PM by ClassicalSoul.)
Hi,
I came across this and was wondering about the mechanics:
Thanks
-----
I got it! I figured I may as well answer my own question. The key is in understanding the true nature of the second example. In the first iteration, y = 1; in the second, y = 2; and so on, and what you end up 'keeping' is the final iteration, where y = 100. Now apply that same logic to the first example: in the first iteration, x = 1; but in the second, x = 3; and in the third, x = 5; and so on, until you reach the final iteration where x = 5050 -- and that is the iteration you 'keep'.
I came across this and was wondering about the mechanics:
>>> x = 0 >>> r = range(1, 101) >>> for n in r: ... x = n + x ... >>> x 5050Comparing it to this though (which I somewhat understand), I'm not getting much of a sense of what's going on:
>>> x = 0 >>> r = range(1, 101) >>> for n in r: ... y = n + x ... >>> y 100
Thanks
-----
I got it! I figured I may as well answer my own question. The key is in understanding the true nature of the second example. In the first iteration, y = 1; in the second, y = 2; and so on, and what you end up 'keeping' is the final iteration, where y = 100. Now apply that same logic to the first example: in the first iteration, x = 1; but in the second, x = 3; and in the third, x = 5; and so on, until you reach the final iteration where x = 5050 -- and that is the iteration you 'keep'.