Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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,704 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