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 - printing the wrong items?

Just trying to do this exercise and it is telling me that the program didn't print the right items. No idea why... any suggestions?

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

      if i == "STOP":
        break

2 Answers

The way your code is currently written, it prints every item in items (even if that item is "STOP")

def loopy(items):
    for i in items:
      print(i)              # this needs to be checked before it gets printed

      if i == "STOP":   # these 2 lines look good. but it checks for "STOP" after your print(i) so the order is wrong
        break
      # you will need an "else" part of the if-statement
Andrew Winkler
Andrew Winkler
37,739 Points

Your if block has to be taken into consideration before the loop print(i). You've almost got it, but stop and think for a second. The condition needs to be taken into consideration before the function prints, otherwise, you'll be printing an additional i value of "STOP" before the function break.

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