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 trialEric Watters
2,615 PointsI am not sure how to complete this for loop.
I need you to help me finish my loopy function. Inside of the function, I need a for loop that prints each thing in items. But, if the current thing is the string "STOP", break the loop.
def loopy(items):
# Code goes here
for items in loopy:
print(loopy)
if items == 'STOP':
break
Chris Campisi
1,226 PointsStephan put your answer under the answers sections so I can upvote it :)
2 Answers
Chris Campisi
1,226 PointsThere were a couple of things:
def loopy(items):
# Code goes here
for x in items:
print(x)
if x == 'STOP':
break
items = ['soup', 'food', 'STOP', 'chicken']
loopy(items)
First how you were using the For loop was wrong. You're not looping through a function. You're iterating through items. And you need to look at one specific item at a time. For x in items does this. Where x is the current item. From there it is the only item that you are comparing against in the rest of the function.
you print(x) not the entire function loopy or the entire list items. you compare x, which is the current item in the list, to STOP.
Further you should change the if structure to:
if x == 'STOP':
break
else
print(x)
move the print x to an else statement so it doesn't actually print the STOP. But I left things as they were for you as much as possible so you can make the choice.
Eric Watters
2,615 PointsBoth of these don't allow me to pass the code challenge still.
kenyetta watt
6,899 Pointsnone of you guys answers were right
Stephan Livera
4,146 PointsStephan Livera
4,146 PointsHi Eric,
I was able to get it by putting the break BEFORE the print, see my solution below: