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

what is wrong with this code?

def loopy(items): # Code goes here for item in items: print(item)

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

        if item  == 'STOP':
            break

1 Answer

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there! Honestly, there's not a lot wrong with your code, but it's a little out of order and doing something it's not intended to do. It looks at an item in the items list and prints it. Then it checks to see if it was "STOP". If it was "STOP", it breaks. But this also means that it will still print out "STOP" because the print part happens first.

Try reordering your code a bit so that it only prints out the items that are not "STOP". To be clear, you can fix this by putting your print after the if statement.

I think you can get it with this hint, but let me know if you're still stuck! :sparkles:

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

        print(item)

still doesn't work!!!

Jennifer Nordell
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi! I added some markdown to your code so that it's easily readable. It would be interesting to know what kind of error you're receiving. There's the possibility that you're getting an indentation type of error as a Python file may not use both spacing and tab indentation in the same file. Your code is almost identical to what I'm using to pass, but please note that this code will not pass Step 1. You will need to remove the if statement altogether for the code to pass Step 1.

Here was my passing code:

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

Hope this helps! :sparkles: