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

Mark Kohner
Mark Kohner
1,121 Points

python loopy break; not clear where I am going wrong on this! Break/print

finish loopy , print each item, break on string 'stop'

def loopy(items): for items in loopy: print(items) if items == 'STOP' break i am not sure about indentation in functions, but I have tried several other ways and do not get any feedback besides 'Bummer! Try again!' .

breaks.py
def loopy(items):
    # Code goes here
    for items in loopy:
    print(items)
      if items == 'STOP'
        break
Grigorij Schleifer
Grigorij Schleifer
10,365 Points

Hey,

the print statement belongs to the for loop. So if the input is not STOP the items will be printed.

You have to check if it is STOP first and if yes break the code and only after this print your item.

Makes sense?

Abdallah El Kabbany
Abdallah El Kabbany
2,042 Points

oh got it so what you are trying to say that it is going step by step right? in other means:

  • check if it is stop
  • if yes stop
  • if no print the value

Right?

Grigorij Schleifer
Grigorij Schleifer
10,365 Points

Hey,

you are super right !!!

def loopy(items):
    for item in items:
        if item == "STOP": # check if it is stop
            break # if yes stop
        print(item) # if no print the value

:smiley:

2 Answers

Grigorij Schleifer
Grigorij Schleifer
10,365 Points

Hi Mark,

just move the if block with the break statement in the scope of the for loop.

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

Grigorij

Abdallah El Kabbany
Abdallah El Kabbany
2,042 Points

but why do i have to bring the print function below the if condition what is the problem if i have it above it?

Mark Kohner
Mark Kohner
1,121 Points

why do we use a new 'iterator' (item in items) instead of using the parameter for the function? I am confused on this.