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

Mike Coleman
Mike Coleman
3,938 Points

problem with breaks.py

This code runs in my python interpreter and does what the I believe the challenge is asking for (iterate through a list until "STOP" is found).

But it won't pass the challenge. Any thoughts?

breaks.py
def loopy(items):
    # Code goes here
    for thing in items:
        if thing == "STOP":
            break

        print thing

2 Answers

Mike, close! But you need an else, and print is a method:

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

I like jcorum's answer! However, you don't even need an else, like this:

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

This is because first, it checks if thing == "STOP", and if so, it will break out of the loop, not allowing the other code in the loop (after the break keyword) to run. So if I run this:

for x in range(10):
    print(x)
    break
    print("Hello!")

Python will only print out "0" (remember, Python starts counting form 0) and the loop will break, and also not running the print("Hello!") part.

Hope that helps!