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 trialAli S
2,605 PointsI am having some trouble indenting for loops.
My task is: I need you to help me finish my loopy function. Inside of the function, I need a for loop that prints each thing in items.
Thank you
def loopy(items):
for items in loopy:
print items
2 Answers
Manish Giri
16,266 PointsWhen you iterate through a list, you want to assign a temporary name to each item in the list, which you can then use in the loop. Example -
items = [1, 2, 3]
for i in items:
# now i refers to each item in items, one by one
Now coming to your code, there are some problems -
def loopy(items):
for items in loopy:
print items
-
loopy
is the name of your function, not the name of your list, sofor items in loopy
is wrong. - Your actual list is what's passed into your function -
items
. So you should iterate overitems
. - Name your temporary variable something else, other than
items
, becauseitems
refers to the list itself. So, for example, you use the variablex
-for x in items:
Now give it a shot!
Ali S
2,605 PointsThank you for the advice