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

def loopy(items): # Code goes here for x in items: print(x) if x =='STOP': break

what is wrong with my code?

breaks.py
def loopy(items):
    # Code goes here
    for x in items:
        print(x)
    if x =='STOP':
        break 

Most likely because the "break" is outside the "for" loop. Breaks are related to a FOR loop. Put the last two lines before the print(x) , indented appropriately and it should work.

As it stands, the "For" loop means: For every member of items, print it. (BTW, This includes even the word STOP - no checking is done in the loop). When you come to the IF condition statements, python is finished with the loop and does not know what you want to break from.

1 Answer

Tim Lee
Tim Lee
968 Points

I have figured out your error. Logically it seems like it makes sense, but in python you have to remember that indentation makes a big difference. Right now, your if statement and the break statement is outside your for loop. Put them inside by simply indenting both the if statement and the break command inside the if statement and you should be good to go.