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 trialRoxanne Lee
2,612 PointsShopping list, break question
I'm trying to answer the questions for the shopping list app. https://teamtreehouse.com/library/python-basics/shopping-list-app/break What am i doing wrong here?
def loopy(items):
# Code goes here
for items:
print(items)
if items == "STOP":
break
2 Answers
Justin Horner
Treehouse Guest TeacherHello Roxanne,
You're close! You'll want to have a variable that represents the current item in the iteration. To get that, the for loop would start like this.
for item in items:
Then you want to first check if we need to break before printing the item, otherwise the code would print "STOP" and then break.
for item in items:
if item == "STOP":
If break is called, then the following lines will not be executed, therefore, the next line needs to be the print call. You want to print the individual item, not the items list.
for item in items:
if item == "STOP":
break
print(item)
The result is the following:
def loopy(items):
# Code goes here
for item in items:
if item == "STOP":
break
print(item)
I hope this helps.
Jayden Hassan
3,099 PointsI don't understand why my piece of code wasn't working because mine was the same but
Thank You
Alexander Davison
65,469 PointsAlexander Davison
65,469 PointsGood job explaining Justin Horner!
Justin Horner
Treehouse Guest TeacherJustin Horner
Treehouse Guest TeacherThank you, Alexander Davison :)