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

Need help with the quiz on BREAKS.

Can somebody tell me whats wrong with this code?

breaks.py
def loopy(items):
    # Code goes here
    if items == "Stop":
        break
    for item in items
    print(items)
matthew manning
matthew manning
22,929 Points

def loopy(items): # Code goes here if items == "Stop": break for item in items : << you are missing a colon here print(items) << #indent the print

5 Answers

To clear up all the confusion here. This is the correct code.

def loopy(items):
    # Code goes here
    for item in items:
        if item == "STOP":
            break
        print(item)

You're using a break statement on a if condition, this is not allowed in python. Break statements can only be used in for and while loops. I would refactor your code so that the if condition is inside the for loop. That way if the condition is met it breaks out of the for loop all together.

https://docs.python.org/3/tutorial/controlflow.html#break-and-continue-statements-and-else-clauses-on-loops

Thanks, I made the correction to my code but, it still isn't working.

As Mathew stated, you're also missing a colon and an indent in your for loop. I would take a look at the example I pasted on the link. Also please post your new code.

matthew manning
matthew manning
22,929 Points
def loopy(items):
    # Code goes here
    if items == "Stop":
        break
    for item in items:    <<
        print(items)        <<

You need the if condition to be inside the for loop. Once it's inside the for loop then break statement will work.

Thanks for all the help everybody. Sorry Im so late to reply, but thanks for all the help.