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

Need help with adding a break to a loop

I'm not sure why my code isn't working properly. Do I maybe have it in the wrong area? Do I need to create a new variable before I'm able to break the loop? Please help!

breaks.py
def loopy(items):
    for item in items:
        print(items)

    if item == 'STOP':
    break


# Code goes here

1 Answer

Hi Steve,

You need to indent the break by a further four characters. And also the if should be indented a further 4 spaces too as it is inside the loop. So the break command will be indented 12 spaces in total.

Also, it should come before the print command as the loop should break prior to STOP being printed.

Steve.

Thank you very much Steve for the help! It looks like I had the right idea just needed to know the placement of the code and to add my spaces.

Absolutely - your code was correct but lacking some indenting.

One things that surprised me was that your print command passed the code challenge. You are looping over items, and at each pass, you are storing each element of items in the variable item. You could equally call that variable i or whatever you choose.

Your code then prints the iterable, items, rather than the individual element, item. I think that's a technical issue with this challenge but I think it is worth noting. So, just as you tested item to be equal to 'STOP', similarly, the output should use item, rather than items.

I hope that makes sense!

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

Interestingly, and incorrectly, the first part of this challenge can be passed with:

def loopy(items):
    print(items)

I've notified support of this apparent bug.