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 trialianmckellar
4,344 PointsI'm stuck on the coding challenge, "Break"
Hi All, So I've been stuck on this challenge for about 24 hours now. Last night, and this morning up until now I've been working the code every way I can. What I think the problem is, is that I don't have any user input. Do I need user input for a code challenge though?
Here is the prompt to 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." Thanks! IM
def loopy(items):
# Code goes here
for items in loopy:
print(items)
if items == "STOP":
break
3 Answers
Jennifer Nordell
Treehouse TeacherWell you're close and I can see you've worked on it. In fact, you may have overworked it
Our function is named loopy and it's being sent a list of "items". Whatever that may be. And no, you don't need the input. Treehouse is going to send that. In fact, if you were to write any code that would overwrite what Treehouse is sending in, it would fail.
I'll show you the code, but I suspect you'll understand when you see it.
def loopy(items):
# Code goes here
for item in items:
if item == "STOP":
break
print(item)
So first we define loopy that's going to take a list sent in by Treehouse named "items". Now I said for every item in items. But you could say for every x in items or y in items or whatever you want to call it here. If that item is equal to "STOP" then break. This is where most people get tripped up, because they've written it after the print. If you do that, it will still print STOP and then break, which isn't what we're after. Then if the thing wasn't STOP... print the item. Hope that clears some of it up for you!
Luke Glazebrook
13,564 PointsHi Ian!
Your code is almost there! Take a look at your loop and what you are iterating over. In the challenge you are being asked to loop over the content of 'items' but in your loop you are going through the contents of 'loopy' which is actually null as it is the name of the function.
The following loop will work fine and should help you solve the challenge!
for(item in items):
Feel free to let me know if you have any more problems with this challenge!
Edit: Also like Jennifer Nordell said you need to remember to print the item after you have checked whether the loop should be stopped. Good spot Jennifer!
ianmckellar
4,344 PointsYour code makes sense, for awhile I was trying to use the item in items. Then I was worried about item not being defined. Thanks for both your help!
Luke Glazebrook
13,564 PointsNo problem! I am glad that we could help you out Ian. Good luck and keep coding :)