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

oliverchou
oliverchou
20,886 Points

What's wrong with my code? Is anything missing in the challenge?

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

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

3 Answers

Hey Oliver,

First, you'll want to select the correct list in your for loop, which in this case is 'items'. my_list is not used in this challenge. Then, I would suggest to first check if the item is assigned the string, "STOP". Otherwise, continue to print the items in the list.

def loopy(items):
    # Code goes here
    for item in items:
        if item == "STOP":
            break
        else:
            print(item)
Steven Parker
Steven Parker
231,269 Points

:information_source: You don't really need an "else", since the break will end the loop.

Hey Steven,

Good point. It just makes sense to me if you consider the function in plain English.

If the item is equal to "STOP", stop the loop. Else, print the next item. Though I understand your point.

Steven Parker
Steven Parker
231,269 Points

:point_right: You probably want to check for "STOP" before you print.

Otherwise, "STOP" will always be shown as the last item.

I'll bet you can get it now without an explicit spoiler.