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 trialKennedi Armstrong
883 PointsNot sure what is wrong here
Oops, I forgot that I need to break out of the loop when the current item is the string "STOP". Help me add that code!
def loopy(items):
# Code goes here
for thing in items:
print(thing)
if thing == "STOP":
break
1 Answer
andren
28,558 PointsPython groups code based on how they are indented (spaced). By placing your if statement on the same level as the for loop it is considered to be outside of it, and as such it won't run until after the loop has finished.
You therefore need to indent the code so that the if statement is within the loop just like your print statement is currently. It's also worth noting that the challenge wants the if statement to be above the print statement. That is something the instructions are not super clear about but is one of the requirements to complete the challenge.
Kennedi Armstrong
883 PointsKennedi Armstrong
883 PointsGot it. Thank you!