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 trialKhaleel Yusuf
15,208 PointsIsn't this code correct?
Isn't this code correct? I've tried many times but it doesn't work. I would like it if you gave me the code.
def loopy(items):
# Code goes here
for thing in items:
print(items)
if items == "STOP":
break
3 Answers
Valentin Famelart
19,190 PointsHello there!
First you are looking on a collection but prints this collection instead of the items you get. This is certainty what you want to do.
for item in items:
print(item)
Second, you seem to have a problem here with your if statement that checks if the collection is equal to a value. Which will be false/wrong. And I assume that your want to break your for loop if one of the item in items
is equal to "STOP"
.
Lastly, you try to use break outside of a loop. Which will not work. Your if block must be indented once more.
Something along those lines could do the work:
def loopy(items):
for thing in items:
if thing == "STOP":
break
print(thing)
# Now you're out of your for loop
# code some more.
Khaleel Yusuf
15,208 PointsIt says: "Bummer! Didn't find the right items being printed."
Valentin Famelart
19,190 PointsOh I see. Check my initial anseer, I'll update it.
Khaleel Yusuf
15,208 PointsThat didn't work either.
Valentin Famelart
19,190 PointsWhat's the exact error? There's probably a problem related to the type of thing
or items