Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialRoger Marin
440 PointsHow do I correctly break out of a for loop in python? Stuck on a problem right now
I am trying to get past this task but I can't seem to figure out the proper code. When I do it workspace I receive the error "syntax error: break outside loop"
Please help.
def loopy(items):
# Code goes here
for item in items:
print (item)
if item == "Stop"
break
1 Answer
Steven Parker
231,275 PointsRight now, your "if" statement (and the "break" in it) are not part of the loop because they are not indented far enough. Everything inside a loop must be indented more than the "for" line.
But also if you want to stop before you print out the word "STOP", you'll need to re-arrange the lines so the print happens after the test and break.