Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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.