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 trialWilliam Brown
1,303 PointsThis is foolish My code is correct why am i not getting it right this is dumb.
Challenge Task 2 of 2
Oops, I forgot that I need to break out of the loop when the current item is the string "STOP". Help me add that code!
i did all that but nothing happens
def loopy(items):
for item in items:
print(item)
if item == "STOP":
break
loopy("STOP")
8 Answers
miikis
44,957 PointsHi William,
You're so close, man!. But, you have a logic error: calling print() before your if statement defeats the purpose of the if statement. Let me know if that helps.
Also, there's no need to actually call loopy() at the end; the Challenge never asked you to do that.
Jason Prince
5,113 Pointsdef loopy(items): # Gives a name to the function and provides the list (items).
for item in items: # Each item in the list will be assigned to the item variable.
if item is "STOP": # If an item in the items list is "STOP"...
break # stop the loop.
else: # If an item in the items list is anything but "STOP"...
print(item) # print the item.
Kanthad Yangsattha
946 Pointslife saving, my friend. I didn't know that I have to use "else".
Max Krill
645 Pointsnice one! thanks
Rosario Gomez
10,678 PointsHi Mikis, thank you for your answer!
It does makes sense, unfortunately I still can get this challenge right =(
I changed my code into this:
... def loopy(items): if item == "STOP": break for item in items: print(item) ...
But I'm still getting an error.
Rosario Gomez
10,678 PointsHere's the code:
def loopy(items):
if item == "STOP":
break
for item in items:
print(item)
timoleodilo
1,438 Pointsstill with ERROR
mone rafiatou
896 Pointspls someone can pls help me?? i can't find the correct answer, this is my code: def loopy(items): for item in items: if item == "STOP": break for item in items: print(item)
ajay dalal
Courses Plus Student 555 Pointsdef loopy(items):
Code goes here
for item in items: if item =="STOP": break else : print(item) this is the answer but place indents properly if indent ="properly"; code runs else: error
Dimitri McDaniel
8,718 Pointserm.. why is it
for item in items:
when the part before was
for loopy in items:
Rosario Gomez
10,678 PointsHi everyone! I'm stuck in this same code challange and I don't understand what I'm doing wrong, here's the code I've submitted:
def loopy(items):
if items == "STOP":
break
for item in items:
print(item)
Could anyone help me, please?
Thanks in advance =)
miikis
44,957 PointsHey Rosario,
Your indentation is all over the place β but that may be because of the copy-and-paste. Besides that, you have a few logic errors. The Challenge asked you to loop through each item then β while still in the loop β check if that item is equal to the string "STOP." Whenever a given item is equal to the string "STOP," you want to break out of the loop; otherwise, you want to print the item. Let me know if that makes sense.
William Brown
1,303 PointsWilliam Brown
1,303 Pointsthank you very much man
i was so fed up because i read the thing and wrote everything but didn't realize that logic.
that means i could just use a try and catch then instead of an if statement that would be pretty genius based on what i learnt so far.
Eddie Russell
617 PointsEddie Russell
617 PointsHello Mikis,
Thank you. I was having the same issue. I don't think I would have ever figured that out on my own. Did I miss something in a previous lesson? Also, is there a way to go back and look at the code challeges that I've completed? I ask because sometimes while I'm woking on a challenge I try a bunch of different things and when one of them works the challenge ends and I don't always remeber exactly what I did. I would like to be able to study what I did to finish the challenge.
miikis
44,957 Pointsmiikis
44,957 PointsGlad it helped you out, Eddie! And I know what you mean about that uncertain feeling you get when one of your trial-and-error solutions ends up working but you have no idea why. Unfortunately, Treehouse doesn't offer any functionality to students that would allow them to take notes βΒ or save a challenge question. I'll send a feature request to the appropriate people about it for you, if you want.
But, to be honest, I'm not sure it'd do you much good. Programming is a skill-set like any other and you can only really begin to grok the underlying concepts once you start building stuff. Imagine a skill like playing golf. You could read all the Golf for Dummies books you want, you could watch all the professionals do their thing, but until you actually get out there and try to hit that tiny-ass white ball with the skinniest metal stick ever made, you don't really know anything about Golf. Programming is the same way. Until you actually build a fully-functioning program that an end-user can actually use, you won't really understand what building software is all about. I hope that helps. Good luck ;)
Mariya Chorna
5,597 PointsMariya Chorna
5,597 PointsMikis thanks this helps!