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 trialANDY VAN DEN BOS
2,330 PointsShopping_list.py Challenge Task 2 of 2: breaks
So I got the first part okay, but I am missing something on the second part of the question.
its probably supper simple but I cant see it.
def loopy(items):
for item in items:
print(items)
if item == "STOP":
break
2 Answers
Michael Liebegott
3,960 PointsHey there, Andy!
You were close!
The challenge wants you to break the loop if the item in the items list is equal to "STOP" in all caps. This means the "if" statement you have MUST be in inside of the for loop, or else it will only execute after the for loop is completed. You also want the if statement to come before the print() method because otherwise "STOP" will be printed to the screen before the function actually stops.
The code below allowed me to pass the challenge. Take a minute to look it over and notice how the if statement is within the for loop.
def loopy(items):
for item in items:
if item == "STOP":
break
print(item)
What changed?
if item == "STOP":
break
Hope this helps! :-)
Juan Castro
11,322 PointsHello,
your main problem is indentation, the if statement on your code is outside the for loop. Also I think the if should go before the print call to print the items and not the STOP string.
ANDY VAN DEN BOS
2,330 PointsANDY VAN DEN BOS
2,330 PointsThank you Michael, one day i'll get it!