Basic question

Gary Herron gherron at islandtraining.com
Sat May 12 12:49:02 EDT 2007


Cesar G. Miguel wrote:
> I've been studying python for 2 weeks now and got stucked in the
> following problem:
>
> for j in range(10):
>   print j
>   if(True):
>        j=j+2
>        print 'interno',j
>
> What happens is that "j=j+2" inside IF does not change the loop
> counter ("j") as it would in C or Java, for example.
>
> Am I missing something?
>
> []'s
> Cesar
>
>   
Nope. The loop counter will be assigned successively through the list of 
integers produced by range(10). Inside the loop, if you change j, then 
from that point on for that pass through the body, j will have that 
value. But such an action will not change the fact that next pass 
through the loop, j will be assigned the next value in the list.



More information about the Python-list mailing list