[Tutor] Error :SyntaxError: 'break' outside loop
Steven D'Aprano
steve at pearwood.info
Mon Aug 19 12:48:54 CEST 2013
Hello Zoya, and welcome!
On Sun, Aug 18, 2013 at 10:18:06AM -0700, Zoya Tavakkoli wrote:
> Hi everyone
>
> I write this code for color segmentation ,but after Run I recived this
> error:
>
> SyntaxError: 'break' outside loop
>
> I could not resolve that ,could you please help me?
Is the error message not clear enough? You have a 'break' command
outside of a loop. If the error message is not clear enough, please
suggest something that would be more clear. Also, you should read the
entire traceback, which will show you not just the error, but the exact
line causing the problem.
For example, these two are legal:
# Legal
while x > 100:
if y == 0:
break
...
# Also legal
for i in range(1000):
if x > 20:
break
...
But not this:
# Not legal
if x > 20:
break
You can't break out of a loop, if you aren't inside a loop to begin
with.
> while (1):
> _, frame = cap.read()
You have a while loop with only one line. It loops forever, and never
exits. Are you sure that's what you want?
By the way, it is much, much, much easier to see indentation when you
make it four spaces or eight. Good programmer's editors let you set
indentation to four or eight spaces. Even Notepad, which is *not* a
programmer's editor, lets you hit the Tab key and indent.
> hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
Because this line is not indented, it is not inside the loop.
> if k == 27:
>
> break
Again, because the "if" line is not indented, it is outside the loop,
and so the break is illegal.
--
Steven
More information about the Tutor
mailing list