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

Joshua Clark
Joshua Clark
6,186 Points

Trying to understand how to "break" correctly in Task 2

I have tried several different ways to create this code so that it could break. I don't quite understand what I'm doing incorrectly. I have reviewed but can't quite figure this one out. Is there some good help on the community regarding this one?

breaks.py
def loopy(items):
    for item in (items):
        print(item)

while True:
    item = imput("> ")
    if item = "STOP":
        break

2 Answers

Hi, Joshua

You'll want to put everything inside the "loopy" function (be careful of your indentation). You'll also want to put the "if" statement that contains your "break" inside the "for" loop. Here's one way to write it:

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

If the item is 'STOP' then the "for" loop is broken but if it's not 'STOP' then the "for" loop goes on and prints the item. Hope this helps! D.

Joshua Clark
Joshua Clark
6,186 Points

Thank you very much for the help ds1. I now understand that it all had to be within the same function and the indentation must be on point. I realize that what I was supposed to write was a function, and that there must be a "for" and an "if" Statement in order to form my break. Is that correct? I did get the answer correct now.

Great!