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

Kiran Matthews
Kiran Matthews
552 Points

Can someone please tell me what's wrong with my code?!

The question wants me to exit the for loop if the item is equal to "STOP". Can someone debug and tell me whats wrong with the code?

breaks.py
def loopy(items):
    # Code goes here
    for item in items:
        print(item)

        if item == "STOP":
            break
dublinruncommutr
dublinruncommutr
5,944 Points

You might want to make your item uppercase using the .upper() method before performing the == "STOP" comparison (unless your items list has non-string values). Otherwise, this functions as expected.

1 Answer

Your code iterates through the loop printing the item before checking if the item is equal to stop. The challenge doesn't want you to print "stop". To solve this just put the if statement before the print statement like so:

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