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

kabirdas
kabirdas
1,976 Points

Need help breaking loop

I'm a little confused on this one. I put the code in workspaces to see if I can find the error, and all I get is "'break' outside loop."

Here is the challenge:

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!

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

2 Answers

You need to put the if condition inside the for loop. You can only break out of a loop by calling break in the loop itself.

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

I hope this helps. ~Alex

kabirdas
kabirdas
1,976 Points

OHHHHHHH. NOW I get it. I think I was a little confused because the function was called, "loopy." Thanks!

Can you please provide a Best answer? It will add a check mark next to your question :)

Thank you! ~Alex