0

This might be obvious to others.

Using a > 10 breaks the loop as expected, however a == 10 doesn't. Why is this happening? I am using Python 3.5.

The code snippet:

from PIL import Image

im = Image.open('1.jpg')
px = im.load()
width, height = im.size

for a in range(width):
   for b in range(height):
      if a == 10:
         break
      print(a, b)

Edit: I trying to stop the iteration when the image width has reached 10. The output looks like this:

...
9 477
9 478
9 479
10 0
10 1
10 2
10 3
...
Mihai
  • 2,807
  • 4
  • 28
  • 53
  • 1
    What is your output, and what was the expected output? Using that if inside the second loop is a bit strange because a's value doesn't change while in that loop. – RemcoGerlich Dec 22 '16 at 11:02
  • 1
    Your `break` statement breaks out of the inner loop. If you use `a == 10`, it will only break that one time when `a` is 10 and the next iteration of the outer loop will cause it to be run again. What exactly are you trying to achieve? – Arc676 Dec 22 '16 at 11:02
  • What happens if you run `if int(a) == 10` ? – Andriy Ivaneyko Dec 22 '16 at 11:09
  • @AndriyIvaneyko Same things, keeps on going. I think the issues is as Yegers pointed out that I should break within the context of the first loop. – Mihai Dec 22 '16 at 11:11
  • There are quite a few posts on breaking out of outer loops from an inner loop, which are worth looking at i.e. http://stackoverflow.com/questions/189645/how-to-break-out-of-multiple-loops-in-python – moraygrieve Dec 22 '16 at 11:15
  • But how is `10 0` printed out anyway? No matter which loop is broken, the print statement shouldn't be reached when a equals 10. – Robin Koch Dec 22 '16 at 11:16
  • @RobinKoch any of the solutions mentioned above work. The `10 0` is not printed anymore if I break in the context of the first loop. – Mihai Dec 22 '16 at 11:20
  • Thanks, but that doesn't answer my question. – Robin Koch Dec 22 '16 at 19:00
  • Is that the output *before* you inserted the `break` statement in the first place..? – Robin Koch Dec 22 '16 at 19:02
  • Yes, it's the original output for the code in my question. – Mihai Dec 22 '16 at 19:03
  • Read my question again. Your answer is contradictional. I asked if *that* code in your question (*including* the `break` produces *that* output in your question (including the lines `10 0`, `10 1`, ...). Because it should not and it does not on my machine. – Robin Koch Dec 23 '16 at 09:47

3 Answers3

3

You should put the a == 10 in the outer loop because now it will only break the inner loop.

for a in range(width):
   if a == 10:
      break
   for b in range(height):
      print(a, b)

Depending on your needs, you might want to put it behind the for b in range(.. loop.

Yeg
  • 426
  • 1
  • 8
  • 19
3

Move the if outside the inner loop:

for a in range(width):
   if a == 10:
      break
   for b in range(height):
      print(a, b)

You only left the inner loop and the out one kept running; so when a reach 11, the inner loop starting printing again.

With a > 10 you didn't have that problem as all inner loops immediately stopped, but they did all get started.

RemcoGerlich
  • 30,470
  • 6
  • 61
  • 79
2

Rather than using a break statement, you can use min(...) to predetermine how many times to loop, for example:

for a in range(min(10, width)):
    for b in range(height):
        print(a, b)

This run 10 times, with values of a from 0 to 9 - unless the width is less than 10, in which case it loops width times.

FlipTack
  • 413
  • 3
  • 15