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

Stuck and need advice

I have tried all the commented functions and have not been able to get pass the errors.

breaks.py
#Instructions: 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!

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

#Help: Can you please show me in a code format what I am missing. Thank you

1 Answer

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

Hi there! Your first piece of code was actually the closest. I think by the end you'd actually overthought it a bit, so let's take a closer look at the one you had that was really close.

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

This one was really close, but it contained one slight logic error and one syntax error. The syntax error: you forgot a colon on the end of your if statement. The logic error is this. You are printing every item in the list no matter what. This includes the item "STOP". What we want to do is print out every item until we get to stop and then stop printing. So the check for "STOP" should come first.

Take a look:

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

This says "Go through every item in my items list and pull out one at a time. Assign that individual item to the variable item. If the item we're looking at is equal to "STOP", exit this loop. If it's not "STOP", print the item."

Hope this helps! :sparkles:

oh wow, really appreciate the great explanation that you just gave me.