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

Eric Watters
Eric Watters
2,615 Points

I am not sure how to complete this for loop.

I need you to help me finish my loopy function. Inside of the function, I need a for loop that prints each thing in items. But, if the current thing is the string "STOP", break the loop.

breaks.py
def loopy(items):
    # Code goes here
  for items in loopy:
    print(loopy)
      if items == 'STOP':
        break
Stephan Livera
Stephan Livera
4,146 Points

Hi Eric,

I was able to get it by putting the break BEFORE the print, see my solution below:

def loopy(items):
    # Code goes here

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

Stephan put your answer under the answers sections so I can upvote it :)

2 Answers

There were a couple of things:

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

items = ['soup', 'food', 'STOP', 'chicken']

loopy(items)

First how you were using the For loop was wrong. You're not looping through a function. You're iterating through items. And you need to look at one specific item at a time. For x in items does this. Where x is the current item. From there it is the only item that you are comparing against in the rest of the function.

you print(x) not the entire function loopy or the entire list items. you compare x, which is the current item in the list, to STOP.

Further you should change the if structure to:

if x == 'STOP':
  break
else
  print(x)

move the print x to an else statement so it doesn't actually print the STOP. But I left things as they were for you as much as possible so you can make the choice.

Eric Watters
Eric Watters
2,615 Points

Both of these don't allow me to pass the code challenge still.

none of you guys answers were right