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 trialKiran Matthews
552 PointsCan someone please tell me what's wrong with my code?!
The question wants me to exit the for loop if the item is equal to "STOP". Can someone debug and tell me whats wrong with the code?
def loopy(items):
# Code goes here
for item in items:
print(item)
if item == "STOP":
break
1 Answer
Lukas Crockett
3,589 PointsYour code iterates through the loop printing the item before checking if the item is equal to stop. The challenge doesn't want you to print "stop". To solve this just put the if statement before the print statement like so:
def loopy(items):
# Code goes here
for item in items:
if item == "STOP":
break
print(item)
dublinruncommutr
5,944 Pointsdublinruncommutr
5,944 PointsYou might want to make your item uppercase using the .upper() method before performing the == "STOP" comparison (unless your items list has non-string values). Otherwise, this functions as expected.