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

Trouble breaking a for loop in a function.

Having some trouble with the second part of the challenge task. Here's what I have so far:

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

When I ask the challenge to check my work, it says: "Didn't find the right items being printed." I passed the first part of the challenge without any problems and I'm sure that I must be missing something obvious here, but I just can't see it. Any hints?

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

1 Answer

Hey Elizabeth! A couple of things here... First, try changing the name of the variable you are using to iterate over items, at the moment, you are using the same name, so it doesn't work. Basically your code is saying, iterate through items using items, wich doesn't make sense. The correct thinking would be, iterate through items using item. Something like this?

for item in items:
    print(item)

Also, you shouldn't use the break here. Here is what the loop is doing when you put break inside of it. It searches for the first "item" in "items", it prints "item", and then the break tells it to break the loop, basically stop it. So you are just printing one item. If you remove the break, it will iterate all items and print them.

Hope this helped.

Ah! I see!

Thank you so much for the clear explanation. This helps a lot!