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 trialClayton McDaniels
5,788 PointsWhy is the code below not working for this challenge?
def loopy(items): for thing in items: print(thing) if items == 'STOP': break
def loopy(items):
for thing in items:
print(thing)
if items == 'STOP':
break
2 Answers
Steve Hunter
57,712 PointsHi Clayton,
If the user enters STOP, don't print but just break
. So, put your if
before the print
line.
Also, you want to see if thing
equals STOP, not items
.
Steve.
Clayton McDaniels
5,788 PointsSteve, your followup reply makes more sense and is more logical. I thought that should work too, however the challenge accepted just moving the if statement.
Steve Hunter
57,712 PointsYou got lucky, I reckon. I tried it with just moving the statement and it failed. Still, it doesn't matter! Enjoy the rest of the course!
Clayton McDaniels
5,788 PointsClayton McDaniels
5,788 PointsCopying the if before the print line worked! All else is fine! Thank you!
Steve Hunter
57,712 PointsSteve Hunter
57,712 PointsI found that just moving the
if
wasn't sufficient. This doesn't work:Whereas changing
if items == "STOP":
toif thing == "STOP":
meant that the code is comparing the for loop variable which holds each element ofitems
in turn, which isthing
. This code worked:Steve.