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

challenge task 2 of 2 - shopping list app

"Challenge Task 2 of 2

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!"

I just don't get what I am doing wrong, Pls help, here's my code below.

breaks.py
def loopy(items):
    # Code goes here

    for thing in items:
        print (items)
        elif items == "STOP":
            break

3 Answers

Oszkár Fehér
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Oszkár Fehér
Treehouse Project Reviewer

Hi Ojo If we write the code for you, it will not be for your help. Your loop starts ok but before you print() out anything you should check if it contains "STOP"

if thing == "STOP"

if this condition is correct than break the loop. After this check you should print() the rest

print(thing)

elif statement can't start first an if statement, it's a short of else if statement, if there is no if it can't be an elif, I hope this helps you to understand a little bit better the if statement and I also hope it was for your help. Keep up the good coding.

nakalkucing
nakalkucing
12,964 Points

Hi, it looks to me like you've almost got that bit of coding. But instead of an "elif" statement you need and "if" statement, and your "if" statement needs to go before your "print." Like this:

def loopy(items): for item in items: if item == 'STOP': break print (item) Hope it helps. : )

nakalkucing
nakalkucing
12,964 Points

Opps! I didn't realize it would put that out of order.

nakalkucing
nakalkucing
12,964 Points
def loopy(items):
    for item in items:
        if item == 'STOP':
            break
        print (item)