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

How do you use break

How do you use break again?

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

4 Answers

Hey Annika! As Pete explained your going to want to move the if statement up so that "STOP" doesn't get printed, your code should look like this:

def loopy(items):
    for item in items:
        if item == "STOP":
            break
        else:
            print(item)
Pete P
Pete P
7,613 Points

If you would like the loop to break if item is equal to "STOP", your code should look like this:

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

Notice the indents. The 'if' statement should be inside the for loop. This will iterate through items. As soon as item is equal to the word "STOP", "STOP" will be printed to the screen (since it is called before the if statement) and the loop will then break.

I think you probably don't want "STOP" to be printed to the screen. So, the print statement should move to the end of the loop. Try it out and let me know if you're still having trouble.

That didn't help I am still suck

Thank you