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

Python Basics "Break" Task 2 of 2

I cannot seem to figure out what error I have committed (syntax, or otherwise) for the following script (Python Basics, "Break" Task 2 of 2):

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

When I copy this into IDLE and take it for a spin, I get the message that the "break" is outside the loop, but it does not seem to matter how I indent. I have tried indenting the above code many different ways.

I sometimes get a cryptic error message to the effect that not all "items" are being printed, which would point to some sort of loop issue, but where, pray tell?

I may not understand how a "break" works, as well as flow control works.

The task questions can sometimes be ambiguously worded, and I may have missed something.

I based my script on the notes I took from the previous video lesson.

Thank you.

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

3 Answers

Stuart Wright
Stuart Wright
41,118 Points

Your if statement needs to be inside your for loop, and before your print statement. That way if the item = 'STOP', the loop will break and the current item will not be printed.

The other issue is that your current print statement is printing 'items', which is the full list. You only want to print one item at a time, so print(eachthing) is what you need here.

Full code should be as follows:

def loopy(items):
    for eachthing in items:
        if eachthing == "STOP":

Stuart, Thank you for the highly clear explanation. My mistakes look obvious after you kindly pointed them out.

I appreciate your time and help!

Can you please provide Stuart a Best Answer? It will mark your question as answered, and that can be helpful to other students who are willing to help others (like me) tell if a question is answer just by looking :smile:

Thanks! :+1:

Done! Thank you!