0

This is in Java:

for(int i=0; i<10; i++){
    while(i%3!=0)
        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.

i=1
while i<=10:
    while i%3 is not 0:
        i+=1
    print('run')

I could have achieved target by removing internal while and changing the code to i+=3. 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.

Update: Here is program for which I was trying this: https://softwareengineering.stackexchange.com/questions/327908/finding-total-number-of-subarrays-from-given-array-of-numbers-with-equal-max-and

2
  • 1
    Well, you don't increment i once it hits 3, so of course it runs infinitely. Commented Aug 8, 2016 at 18:43
  • 2
    Your outer for-loop is never incrementing i. So, if i starts at 0, it will forever remain 0 due to the lack of an increment. Also, if you're wanting to print out every third number from [0, 10] on a single line, why not just do something like: for i in range(0, 11, 3): print(i, end=' ') Commented Aug 8, 2016 at 18:44

5 Answers 5

3

There is no need for two loops in your example.

while i<=10:
  if i%3 == 0:
    print(i+ " ")
  i++

Your code runs infinitely because you never increment i when it equals 3

Sign up to request clarification or add additional context in comments.

Comments

2
i = 0
while i < 10:
    while i % 3 is not 0:
        i += 1
    print(str(i) + " ")
    i += 1

4 Comments

@xploreraj Then the top answer I gave is what you want.
This seems like an exact match of the java version, isn't it?
@Aguy It is. You basically can't get any closer than this in Python.
Dont know where mind was when I missed outer increment, without that as @Dallan pointed, it gets stuck forever. Probably hung late night!
1

Another option is to just use a list comprehension:

print(' '.join([i for i in range(10) if i % 3 == 0])

1 Comment

Just increment outside of the inner loop. The outer loop is running forever because there's nothing changing the value of i once the inner loop completes. If, however, your desired output is the 0 3 6 9 example you gave, go with @Dallan's answer.
1

OK. Another suggestion for the party. Might be helpful with your actual task:

g = iter(range(10))
for i in g:
    while i%3 is not 0:
        i = next(g)
    print(i)

The main difference is that this will raise StopIteration exception when the inner loop exceeds the range defined for the iterator (i.e. for the outer loop). Might be something desired, or might not.

1 Comment

Interesting. Introduces to me the iter function.
0

From my comment on the OP's question:

for i in range(0, 11, 3): print(i, end=' ')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.