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 trialDaniel Petrov
3,495 PointsFor loop
A bit confused about this one: why would we use a "for loop" where here a while loop should be used?
def loopy(items):
# Code goes here
items = []
for item in items:
print(item)
if item == "STOP"
break
4 Answers
Chris Freeman
Treehouse Moderator 68,441 PointsYou are very close. The if
statement needs indenting to match the print
to become part of the for
loop. Also the if
and print
statements should be reversed so the break
can happen before the print
or the "STOP" will be printed.
# complete answer
def loopy(items):
for item in items:
if item == "STOP":
break
print(item)
Edit added completed answer
Evan Colvin
1,190 PointsFor loops are more common.
While loops can go off the rails if you never hit your stopping condition, or you might never use your while loop if the start condition isn't true when it reaches that line.
You can almost always figure out how many times your for loop will run just by looking at the code. From your code, I can see that the maximum number of iterations is the length of the list items. Depending on how you set your while loop condition I might not know that just by looking.
Daniel Petrov
3,495 PointsThanks Evan, that all comes with the experience I suppose.
Daniel Petrov
3,495 PointsThanks Chris, it makes sense but it still says: Bummer! Didn't find the right items being printed.
def loopy(items):
# Code goes here
items = []
for item in items:
if item == "STOP":
break
print(items)
Not sure if you can see the code with the right indentation ... How can I post directly from the challenge black board?
[MOD: added ```python markdown formatting -cf]
Chris Freeman
Treehouse Moderator 68,441 PointsDo not reset the value of items
in the 3rd line. It clears the argument passed to the function.
def loopy(items):
# Code goes here
### items = [] # <-- remove this line
for item in items:
if item == "STOP":
break
print(item) # <-- remove 's'
Daniel Petrov
3,495 PointsAh, still the same message. There must be something wrong as I already tried without that line. Reloaded the page as well but no success ...
Chris Freeman
Treehouse Moderator 68,441 PointsYes. There was typo in the print statement. Should be "item", not "items". Answers corrected.
Daniel Petrov
3,495 PointsYes :)
Joyce Davis
Courses Plus Student 544 PointsJoyce Davis
Courses Plus Student 544 Pointswhy do i not need " else:" after the break?