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 trialDaffa Alif Pratama
2,316 PointsWhat is the code challenge asking, really?
I really dont know what the code challenge requesting. Please take a look at my code: Here is the challenge: 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):
for thing in items:
print(thing)
if thing = "STOP":
break
2 Answers
Chris Freeman
Treehouse Moderator 68,441 PointsYour code is close. Two changes needed: move the if
inside the for
loop by indenting it. Then move the print
after the if
so it doesn't print if STOP is seen.
Ryan Ruscett
23,309 PointsYes yes,
Add the if inside the for loop. So while looping through it says. For item in items, IF that item is STOP than break or else if that item doesn't say step. It's an else and the else quotes the loop So else: break.
def loopy(items):
for item in items:
if item == "STOP":
break
else:
print(item)
You followed the question well but may have gotten lost at the the BUT lol. The BUT always gets us. Let me know if you don't understand any part of this and we will try to explain.
Chris Freeman
Treehouse Moderator 68,441 PointsThis can be simplified to:
def loopy(items):
for item in items:
if item == "STOP":
break
print(item)
Ryan Ruscett
23,309 PointsYes yes this is true, but we in reality you could solve this outside a function or in another with list comprehension much easier. Although, Chris is right, it's an easier way to write it which psh, after you right a 1000 of these you look for every shortcut possible lol. Good work!
Daffa Alif Pratama
2,316 PointsThanks for the help, Ryan!
Daffa Alif Pratama
2,316 PointsDaffa Alif Pratama
2,316 PointsThank you!