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 trialCamray Austin
Courses Plus Student 725 PointsNeed help with the quiz on BREAKS.
Can somebody tell me whats wrong with this code?
def loopy(items):
# Code goes here
if items == "Stop":
break
for item in items
print(items)
5 Answers
akhter ali
15,778 PointsTo 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
15,778 PointsYou'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.
Camray Austin
Courses Plus Student 725 PointsThanks, I made the correction to my code but, it still isn't working.
akhter ali
15,778 PointsAs 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
22,451 Pointsdef loopy(items):
# Code goes here
if items == "Stop":
break
for item in items: <<
print(items) <<
akhter ali
15,778 PointsYou need the if condition to be inside the for loop. Once it's inside the for loop then break statement will work.
Camray Austin
Courses Plus Student 725 PointsThanks for all the help everybody. Sorry Im so late to reply, but thanks for all the help.
matthew manning
22,451 Pointsmatthew manning
22,451 Pointsdef loopy(items): # Code goes here if items == "Stop": break for item in items : << you are missing a colon here print(items) << #indent the print