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

Stivan Radev
Stivan Radev
1,475 Points

I need help with this challenge

Why is my code not working?

breaks.py
def loopy(items):
    for item in items:
        print(item)

    if item == 'STOP'
        break

You have to consider the following examples: ["first_thing", "second_thing", "STOP", "another_thing"]. Would your code print the word "STOP", would your code print "another_thing"? Is that how you want it to work or do you want it to stop and not print the word "STOP". Also, think about code blocks. Your for loop and your if statement are at the same level of indentation. Based on this when will it check your if statement? Think about when you want to do the checking for "STOP". These are some hints. I have not quite given you the answer because you are close you just need to think of indentation and the position of when you check for "STOP"

1 Answer

You have to consider the following examples: ["first_thing", "second_thing", "STOP", "another_thing"]. Would your code print the word "STOP", would your code print "another_thing"? Is that how you want it to work or do you want it to stop and not print the word "STOP". Also, think about code blocks. Your for loop and your if statement are at the same level of indentation. Based on this when will it check your if statement? Think about when you want to do the checking for "STOP". These are some hints. I have not quite given you the answer because you are close you just need to think of indentation and the position of when you check for "STOP"

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