+ 6

Why do I get a SyntaxError for this code (Python)?

I'm a beginner and having trouble with the "break" function, so I'm playing around with codes with "break". However, no matter what I try, I always get an error (usually SyntaxError) or "No Output". I would like to know what I'm doing wrong and how to correct it. The code is something like this: for num in range(1, 25, 3): if num <= 20: break print(num)

2nd Nov 2019, 5:59 PM
Craig
40 Antworten
+ 12
Craig The reason why it does not give output is because it will be broken already when num was 1. That broke the whole for loop and num = 4, 7, 10, 13, 16, 19 and 22 were all ignored, and the code could have continued after the for loop statement. In this sample "Loop broken" would be printed: for num in range(1, 25, 3): if num <= 20: break print(num) print("Loop broken")
2nd Nov 2019, 7:22 PM
Seb TheS
Seb TheS - avatar
+ 8
'Something like this' isn't good enough. This code snippet works just fine. Please show us the exact code where the error occurs.
2nd Nov 2019, 6:08 PM
HonFu
HonFu - avatar
+ 4
Thomas Guyonvarch You might be confusing return and break. break can always be used in loops. return can be used in functions (not in lambda defined functions)
2nd Nov 2019, 7:15 PM
Seb TheS
Seb TheS - avatar
+ 4
Craig, it works the same whichever iterable you use. It just depends on you knowing what you're doing. Your first example was just not set up meaningfully. You generated a list of numbers going 1, 4, 7 and so on - and then right in the first run of the loop you say: If the number is equal or lower than 20 (of course 1 is lower than 20!), stop the whole business. Obviously nothing happens. It doesn't have to be obvious to you - it takes practice. But it is thoroughly logical.
3rd Nov 2019, 1:19 AM
HonFu
HonFu - avatar
+ 3
Maybe the use cases you saw were a bit far-fetched. Basically (especially in Python) a loop often means that you're walking through a bunch of stuff. Let's say, you have a very large list of strings and want to work with only those that only consist of letters. Then you could loop over the whole list and write a condition (pseudo-code): if not all letters of that work are alphabetic: continue This basically means: Skip this one. So the loop goes directly into next round. Another example: A quiz situation. A user is asked hundred Spanish words and has to give the English translations. If they go through the whole set, they win, but if they make three mistakes, they lose. So you'd loop over the whole list, but you'd have two conditions in the body: if input wrong: mistakes += 1 if mistakes == 3: break So break ends the loop *for good*.
2nd Nov 2019, 9:14 PM
HonFu
HonFu - avatar
+ 2
You can make: Def work(): For num in range(1,25,3): if num <=20: break print(num) work()
2nd Nov 2019, 6:15 PM
Thomas Guyonvarch
Thomas Guyonvarch - avatar
+ 2
I think you are confused by the break and return elements
4th Nov 2019, 6:07 AM
SYRIS°
SYRIS° - avatar
+ 2
This I think you are trying to do without using the break! num = list(range(1, 25, 3)) print(num)
4th Nov 2019, 10:03 AM
Lloyd L Conley
Lloyd L Conley - avatar
+ 2
Gleem Gleem Yes, that's correct. Now I know why, when I got the error, the error was pointing to the <= symbol. I'm learning from my mistakes. 😊
4th Nov 2019, 12:14 PM
Craig
+ 2
Harsh Sharma That's what I wanted the code to do; stop before reaching 20. That's why I used <= 20. Sadly, now I know that doesn't work because 1 is less than 20, and the code won't output anything.
4th Nov 2019, 7:46 PM
Craig
+ 1
HonFu I get the error on line 2 and points to the <= symbol.
2nd Nov 2019, 6:14 PM
Craig
+ 1
Craig it shouldn't show any error as you have exited the loop because the if statement is true on the very first iteration. print(num) doesn't mean anything (or give an error) as the scope of num is in the loop only.
2nd Nov 2019, 6:49 PM
Saurabh Tiwari
Saurabh Tiwari - avatar
+ 1
Seb TheS Thank you. Changing break to continue and <= to >= gives me the result I was looking for.
2nd Nov 2019, 8:35 PM
Craig
+ 1
HonFu Thank you for the explanation and the example. It's becoming more clear to me. It looks like break doesn't work well with numbers and/or functions like range in Python.
3rd Nov 2019, 12:57 AM
Craig
+ 1
You just need to put 'print' under 'for' not 'if' for num in range(1, 25, 3): if num <= 20: break print(num) And by the way the break will be executed in the first loop because num=1 which is <=20 So in your code 'print' will never be executed.
4th Nov 2019, 8:54 AM
Omar Ashraf 🇪🇬
Omar Ashraf 🇪🇬 - avatar
+ 1
Manuel Prochnow Okay, I'll remember that for next time.
4th Nov 2019, 11:51 AM
Craig
+ 1
Lloyd L Conley That's exactly what I was trying to do.
4th Nov 2019, 12:19 PM
Craig
+ 1
You should use > instead of < ,because your loop is breaked for first repetition only. If u use > then output will be 1 4 7 10 13 16 19
4th Nov 2019, 4:11 PM
Harsh Sharma
Harsh Sharma - avatar
0
Could the syntax error be caused by that < and = changed places (=<)
2nd Nov 2019, 7:13 PM
Seb TheS
Seb TheS - avatar
0
I don't see anything wrong with this sample: for num in range(1, 25, 3): if num <= 20: break print(num)
2nd Nov 2019, 7:17 PM
Seb TheS
Seb TheS - avatar