xploreraj xploreraj - 1 year ago 80
Java Question

How to affect outer loop variable from inner loop in Python?

This is in Java:

for(int i=0; i<10; i++){
System.out.print(i + " ");

This will output:

0 3 6 9

I am trying to achieve similar code block in Python 3. I am not able to.

In the outer loop, I can not use range because it causes iteration on whole list I read somewhere I think. So, I am trying below, but it fails dangerously, running infinitely.

while i<=10:
while i%3 is not 0:

I could have achieved target by removing internal
and changing the code to
. But the program I am trying to make has important conditions so it has to be there. There has to be two loops and based on inner loop condition matching, I am incrementing the iteration variable, so when I break and process some program output, then the parent loop should start iterating from where I left off in inner loop. Above is just an example I could think of to share the issue. I need suggestion on how can I replicate the changes as described in Java code in Python.

Answer Source
i = 0
while i < 10:
    while i % 3 is not 0:
        i += 1
    print(str(i) + " ")
    i += 1
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download