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!
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
Stuck and need advice
I have tried all the commented functions and have not been able to get pass the errors.
#Instructions: Oops, I forgot that I need to break out of the loop when the current item #is the string "STOP". Help me add that code! #def loopy(items): #for item in items: #print(item) #if item == 'STOP' #break #def loopy(items): #for item in items: #if items == 'STOP' #print(item) #break def loopy(item): while True: if item == 'STOP' break for item in items: print(item) #Help: Can you please show me in a code format what I am missing. Thank you
Jennifer NordellTreehouse Teacher
Hi there! Your first piece of code was actually the closest. I think by the end you'd actually overthought it a bit, so let's take a closer look at the one you had that was really close.
def loopy(items): for item in items: print(item) if item == 'STOP' break
This one was really close, but it contained one slight logic error and one syntax error. The syntax error: you forgot a colon on the end of your
if statement. The logic error is this. You are printing every item in the list no matter what. This includes the item "STOP". What we want to do is print out every item until we get to stop and then stop printing. So the check for "STOP" should come first.
Take a look:
def loopy(items): for item in items: if item == 'STOP': break print(item)
This says "Go through every item in my items list and pull out one at a time. Assign that individual item to the variable
item. If the
item we're looking at is equal to "STOP", exit this loop. If it's not "STOP", print the item."
Hope this helps!