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

Jorge Gimeno
Jorge Gimeno
5,956 Points

In the breaks.py challenge, none of the code snippets seem to work. The error message gives no reason why.

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

Was the code I tried. The website only gives me back the "Bummer! Try again!" message with no details.

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

1 Answer

Grigorij Schleifer
Grigorij Schleifer
10,365 Points

Hi Jorge,

try this code:

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

The for loop goes first (you are completely right), but before printing the result the if condition should check whether x is STOP or something else. If x is equals to STOP then the loop will break x will not be printed. If x is not equals STOP the loop goes on and x will be printed.

It is possible that 'STOP' is on index 0 like this:

items = ['STOP', 'item_one', 'item_two']

So the loop will enter items see STOP and "break".

In my first answer I forgot to put parenthesis for the print method. Sorry for that :(

I hope this is a better answer

Grigorij

Jorge Gimeno
Jorge Gimeno
5,956 Points

There was no error message, so all I can come up with is that the print statement raised an error without the parenthesis. Thanks for your help!

-Jorge