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 trial

Python Python Basics (2015) Shopping List App Break

What 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
MOD
Chris Freeman
Treehouse Moderator 68,454 Points

Your 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.

Thank you!

Ryan Ruscett
Ryan Ruscett
23,309 Points

Yes 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
Chris Freeman
Treehouse Moderator 68,454 Points

This can be simplified to:

def loopy(items):
  for item in items:
    if item == "STOP":
      break
    print(item)
Ryan Ruscett
Ryan Ruscett
23,309 Points

Yes 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!

Thanks for the help, Ryan!