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

Help guys i am stuck here for a week. I need your help. break statement

so this code is supposed to break out of printing items loop when it comes across the string "STOP".

breaks.py
def loopy(items):
    for things in items:
        if things == "STOP":
            break
        else:
            print(items)

2 Answers

Jason Anders
MOD
Jason Anders
Treehouse Moderator 145,858 Points

Hi Victor,

You have all the correct syntax and indentations! :thumbsup: You are, however, printing the wrong thing. You are using things to iterate over the list. With each iteration, things holds an item in the list, and this is what needs to be printed, but you seem to be trying to print the whole list by using the variable items. Once you change that to the temporary variable things, the task will pass. :)
I feel your confusion for Task two stems from the fact that Task one passed the code when it was incorrect. Task one should not have passed with print(items). (Tagging Rob Allessi. Sorry Rob, not sure who I would tag for this course now).

An added note: When you are using for loops, the convention is to use the singular version of the variable name for the list being looped through. This also helps eliminate any possible confusion (as I believe was the case here) as to which variable is holding what.
In this case if you had done for item in items, you would know that item is holding one item from the list that is holding many items. Make sense?

Aside from that... excellent work so far!! :) :dizzy:

Victor,

As Jason said, notice what you are printing in the else statement. You're iterating over the items, and what do you want to print each time you iterate? The answer's in your for statement.

/james