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 trialEddie Martinez
727 PointsI'm having trouble with this for loop and break. Not sure where I can post my current code though.
What am I missing here? I copied this code into Workspaces and it seems to be working.
def loopy(items):
for item in items:
if items == 'STOP':
break
else:
print(item)
4 Answers
Michael Hulet
47,913 PointsIf you look closely in the if
statement in that code, you accidentally wrote items
(with an "s"), but I think you meant item
(without an "s"). You want to check if the individual item is "STOP"
, not if the parameter items
is
Eddie Martinez
727 PointsMichael, thanks for the quick response. When I added a input to test it out, it does seem to reach the break statement.
Here's what I did to test it (I removed the else statement to clean it up):
def loopy(items): for item in items: if items == 'STOP': break print(item)
items = input("> ") loopy(items)
Thanks again!
Sorry for some reason it doesn't format the code right in this box.
Michael Hulet
47,913 PointsActually, in my first answer, I made an oversight that caused it to be incorrect, and it seems this does, in fact, work. That being the case, what's the question that you were initially trying to ask?
Eddie Martinez
727 PointsSorry Michael, my question was on the challenge the grader is saying I don't have it correctly written or it's not working: "Bummer! Didn't find the right items being printed." So I thought something was wrong in my code. I must be missing a detail on the challenge that they are looking for. I know these assignments seem to build off each other so I didn't want to miss a concept and continue.
Michael Hulet
47,913 PointsIn the context of the challenge, my first answer is correct. You need to write item
in the if
statement, because you're inspecting the individual item, and not the parameter items
that was passed in. The challenge is passing in an array of strings, and not just a string, so items
will never be "STOP"
Eddie Martinez
727 PointsThank you. I kept racking my brain. I made the correction and it worked!!