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 trial

Python Python Basics (2015) Shopping List App Break

Maciek Stępniewski
Maciek Stępniewski
1,082 Points

break out of loop // halp

whyyyyy

breaks.py
def loopy(items):
    for xoxo in items:
        print(xoxo)
    if xoxo == "STOP":
        break

3 Answers

Steven Parker
Steven Parker
231,236 Points

Your test is not part of the loop. Anything that is in the body of a loop must be indented more than the "for" line itself. And a "break" statement is only allowed inside a loop.

Also, the test and break should be performed before the print or the word "STOP" will be printed out as a list item.

I'll bet you can get it from here without an explicit code spoiler.

Hey there!

You made a very small mistake. Your If statement is actually outside your for loop! Your If statement will only be read once your for loop is completed. You need to add the extra white space before your If statement to ensure that it is contained within your loop. This will ensure that for every item in the items list will be checked to see if it is equal to "STOP".

Also, we want to break out of the loop when the item is "STOP", but we want to do this BEFORE we print that item. This means the break statement must come before the print statement.

Reformatting like so should fix your code:

def loopy(items):
    for item in items:
        if item == "STOP":
            break
        print(item)

I hope that helps.