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 trialClayton Dobbs
1,917 PointsHow to put a break in a for loop in python?
The video lesson immediately preceding this challenge task demonstrated a while loop. The instructions aren't clear on whether I'm supposed to initiate a new list, or if I'm supposed to solicit input() to populate 'items.'
Does the loopy(items) function need a separate list to be initialized for the 'items' to go into?
def loopy(items):
items = []
for items in loopy():
if items = "STOP":
break
else:
print (items)
loopy("duck", "STOP")
2 Answers
Chris Jones
Java Web Development Techdegree Graduate 23,933 PointsHi Clayton,
You don't need to add a list to pass into the loopy's items argument/parameter. Treehouse has a list that it will pass into it to test your function.
Secondly, since you're not using the items list you created in line 2, you can delete that.
Third, in the third line, you want to loop through each item in the items argument, since that is the object that contains the list. Loopy is just the name of the function.
Lastly, don't forget the "==" instead of a single "=" on line 4. I always forget too, don't worry:)!
Here's what I used to pass the challenge:
def loopy(items):
for item in items:
if item == 'STOP':
break
else:
print(item)
You were close!! Keep it up!
Clayton Dobbs
1,917 PointsThank you @chrisjones22, the other major thing that I was apparently forgetting was that I didn't need to initialize the new variable item
from your line 2. If you reformat your comment as an answer, I'll choose your answer as best answer so that you get credit for it.
Chris Jones
Java Web Development Techdegree Graduate 23,933 PointsNot a problem! Correct, the items list you created in line 2 of your function is not needed.
Sorry, it was late - I guess I clicked Comment instead of Answer:). Thanks!
Chris Jones
Java Web Development Techdegree Graduate 23,933 PointsChris Jones
Java Web Development Techdegree Graduate 23,933 PointsHi Clayton,
You don't need to add a list to pass into the loopy's items argument/parameter. Treehouse has a list that it will pass into it to test your function.
Secondly, since you're not using the items list you created in line 2, you can delete that.
Third, in the third line, you want to loop through each item in the items argument, since that is the object that contains the list. Loopy is just the name of the function.
Lastly, don't forget the "==" instead of a single "=" on line 4. I always forget too, don't worry:)!
Here's what I used to pass the challenge:
You were close!! Keep it up!