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

Camray Austin
PLUS
Camray Austin
Courses Plus Student 725 Points

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)

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

akhter ali
akhter ali
15,778 Points

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)
akhter ali
akhter ali
15,778 Points

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

Camray Austin
PLUS
Camray Austin
Courses Plus Student 725 Points

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

akhter ali
akhter ali
15,778 Points

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.

def loopy(items):
    # Code goes here
    if items == "Stop":
        break
    for item in items:    <<
        print(items)        <<
akhter ali
akhter ali
15,778 Points

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

Camray Austin
PLUS
Camray Austin
Courses Plus Student 725 Points

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