Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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)