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 trialoliverchou
20,886 PointsWhat's wrong with my code? Is anything missing in the challenge?
def loopy(items): # Code goes here for items in my_list: print(items) if items = "STOP": break
def loopy(items):
# Code goes here
for items in my_list:
print(items)
if items = "STOP":
break
3 Answers
jacobproffer
24,604 PointsHey Oliver,
First, you'll want to select the correct list in your for loop, which in this case is 'items'. my_list is not used in this challenge. Then, I would suggest to first check if the item is assigned the string, "STOP". Otherwise, continue to print the items in the list.
def loopy(items):
# Code goes here
for item in items:
if item == "STOP":
break
else:
print(item)
Steven Parker
231,269 PointsYou probably want to check for "STOP" before you print.
Otherwise, "STOP" will always be shown as the last item.
I'll bet you can get it now without an explicit spoiler.
oliverchou
20,886 Pointsthanks a lot~ :)
Steven Parker
231,269 PointsSteven Parker
231,269 PointsYou don't really need an "else", since the break will end the loop.
jacobproffer
24,604 Pointsjacobproffer
24,604 PointsHey Steven,
Good point. It just makes sense to me if you consider the function in plain English.
If the item is equal to "STOP", stop the loop. Else, print the next item. Though I understand your point.