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

Breaks question

for this challenge, do we need to add items in a big list, then print each item in the list?

breaks.py
def loopy(items):

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

4 Answers

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

Hi there! First, Treehouse is going to send in their own list (and we have no idea what it is). So no, you don't have to make your own list.

You have some indentation problems, and you've forgotten a few colons. Remember, we use a colon on the line before we start a new code block.

Also, you have a small logic error. Your code as it is now will look at the item and then print it. Then it will evaluate if it was "STOP" or not. Which means that every list will print out "STOP". We want it to break out before it gets to print out STOP. Take a look at my solution and see if it makes sense:

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

Hope this helps! :sparkles:

Just one more thing to add to your response Jennifer, you forgot to include the else statement after the break.

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

Oh that's strange, my objective wouldn't say Correct until I added the else statement... Why isn't the else required? Thank you!

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

Charles Sells that is indeed strange. If I open up the challenge, and copy/paste my code in, it passes!

But the reason we don't need the else is this: it is going to print every item unless the the item is equal to "STOP". If the item is equal to STOP it breaks from the loop. Otherwise, it just keeps going. In this case, it prints out the item.

Your code works. There's no doubt about that. But so does mine. It's just that in this example it's not strictly necessary. Hope this makes sense! :sparkles:

Thanks! Your profile is amazing by the way. Definitely an inspiration!

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

Awww thank you! I've been working/coding/studying really hard for months :smiley: Now, if I could just get my ideas as organized as my code ... :thumbsup: