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

Abdallah El Kabbany
Abdallah El Kabbany
2,042 Points

my code for this code challenge is not working and i cant find what is it i am doing wrong

help me with what i am doing wrong in this code

breaks.py
def loopy(items):
    # Code goes here
    items = ["apple", "pears", "STOP"]
    for item in items:
      print(item)
      if item == "STOP":
        break

1 Answer

Ryan Ruscett
Ryan Ruscett
23,309 Points

Hey,

You are close, just missing a few concepts. First off, I put your code, then I explained it, and put my code below.

Your Code Explained:

  1. The questions says that a list of items will be passed into the function. "def loopy(items)" This is done for you. You do not need to actually create a list of items with "items = ["apple", "pears", "STOP"]". The items are passed into the function, not defined in the fucntion. Make sense?

  2. You don't need to print the item first. If you print the item first, then break if it = STOP. You are in fact, actually print STOP and then breaking. You need to BREAK if the word is STOP and not actually print it.

def loopy(items):
    # Code goes here
    items = ["apple", "pears", "STOP"]
    for item in items:
      print(item)
      if item == "STOP":
        break

FIXED CODE:

  1. I removed the definition for items, since it's already being passed in and is not needed. I also fixed the logic in the if statement to say if the word is STOP, than break. Don't print it first. Then I used an else to say ok, if the item isn't STOP, than print it. This will print everything up to but not including the word STOP.
def loopy(items):
    for item in items:
      if item == "STOP":
        break
      else:
        print (item)

Does this make sense? If not, let me know and I can try to address additional questions. Thanks!