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

Roxanne Lee
Roxanne Lee
2,612 Points

Shopping list, break question

I'm trying to answer the questions for the shopping list app. https://teamtreehouse.com/library/python-basics/shopping-list-app/break What am i doing wrong here?

breaks.py
def loopy(items):
    # Code goes here
    for items:
      print(items)
    if items == "STOP":
      break

2 Answers

Justin Horner
STAFF
Justin Horner
Treehouse Guest Teacher

Hello Roxanne,

You're close! You'll want to have a variable that represents the current item in the iteration. To get that, the for loop would start like this.

for item in items:

Then you want to first check if we need to break before printing the item, otherwise the code would print "STOP" and then break.

for item in items:
    if item == "STOP":

If break is called, then the following lines will not be executed, therefore, the next line needs to be the print call. You want to print the individual item, not the items list.

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

The result is the following:

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

I hope this helps.

Good job explaining Justin Horner! :thumbsup:

I don't understand why my piece of code wasn't working because mine was the same but

Thank You