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 trialMike Coleman
3,938 Pointsproblem with breaks.py
This code runs in my python interpreter and does what the I believe the challenge is asking for (iterate through a list until "STOP" is found).
But it won't pass the challenge. Any thoughts?
def loopy(items):
# Code goes here
for thing in items:
if thing == "STOP":
break
print thing
2 Answers
jcorum
71,830 PointsMike, close! But you need an else, and print is a method:
def loopy(items):
for thing in items:
if thing == "STOP":
break
else:
print(thing)
Alexander Davison
65,469 PointsI like jcorum's answer! However, you don't even need an else, like this:
def loopy(items):
for thing in items:
if thing == "STOP":
break
print(thing)
This is because first, it checks if thing == "STOP", and if so, it will break out of the loop, not allowing the other code in the loop (after the break keyword) to run. So if I run this:
for x in range(10):
print(x)
break
print("Hello!")
Python will only print out "0" (remember, Python starts counting form 0) and the loop will break, and also not running the print("Hello!") part.
Hope that helps!