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

Christian Geib
Christian Geib
826 Points

Receive error message, seems indentation issue

So this is what I have used:

def loopy(items): # Code goes here for item in items: print(item) if item == 'STOP': break I am quite sure it is an indentation issue but even if I indent the if item loop a bit further I still receive the Bummer error message

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

2 Answers

Greg Kaleka
Greg Kaleka
39,021 Points

Hi Christian,

You've got two issues with your code. 1. is indentation, which you correctly identified. Note that your indentation is wrong in just the right way - it would still pass the challenge because you've just indented one block of code a bit too much. That said, we should still fix it. It should look like this:

def loopy(items):
    # Code goes here
    for item in items:  # no need to indent this further. Still just inside loopy block
        print(item)
        if item == 'STOP':
            break

The second issue is the order of your code. It's not 100% clear from the challenge prompt, but my interpretation - and the code that passes the challenge - is that if the item is "STOP" you should not print that item, but instead break out of the loop immediately. To do this we just need to flip the order of your if..STOP..break and the print statement:

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

Hopefully that clears things up!

Cheers :beers:

-Greg

Christian Geib
Christian Geib
826 Points

Thanks, Greg for your comprehensive and illuminating response. Extremely helpful, it did the trick! Cheers:-)

Thomas McDonnell
Thomas McDonnell
8,212 Points

Hi Christian Geib, You just have some code a little jumbled up. lets word it out!

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

I always find playing with my code in the python shell helps needle out some of these kinds of issues.

Hope this helps:)

Christian Geib
Christian Geib
826 Points

Thanks, Thomas, that is extremely helpful! Thank you very much:-)!