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

David Gabriel
seal-mask
.a{fill-rule:evenodd;}techdegree
David Gabriel
Python Web Development Techdegree Student 979 Points

for loop

Hi all,

Can you please help with my error below: def loopy(items): # for item in items for item in items: if item("items") == "stop": break else: print(item)

breaks.py
def loopy(items):
    # for item in items
    for item in items:
        if item("items") == "stop":
            break
        else:
            print(item)

1 Answer

AJ Salmon
AJ Salmon
5,675 Points

Hi David! There are a couple problems here, the first one being your syntax in your 'if'. You don't need to have ("items") after the word item, because you already clarified that your for loop is being performed on the argument 'items'. Also, loop variables don't work like that, for the same reason. The second issue is that the challenge asks for you to break the loop when the string "STOP" is passed to the function, and you had it break for the string "stop". While they're the same word, they are different strings, due to the case of the letters. It should look something like this:

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

Hope this helps!

Steven Parker
Steven Parker
229,744 Points

FYI: The "else" isn't needed because of the "break" (but it doesn't hurt).

AJ Salmon
AJ Salmon
5,675 Points

Ah, that thought hadn't crossed my mind, but it does make sense. Thanks!