Welcome to the Treehouse Community
The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.
Oscar Villarruel1,450 Points
I'm having a hard time making this for loop work. thank you for any help
i know i can just type in print(items) but its also asking for a for loop. maybe i just don't understand how they work?
def loopy(items): for items in loopy print(items)
The main issue is indeed the structure of your loop. I'll try to give you a quick summary of how a
for loop works.
The way a
for loop works is that it takes a list you specify after the
in keyword. And pulls out each item from that list and assigns it to a variable which becomes available within the loop. The name of that variable is whatever name you type before the
During the first iteration of the loop it pulls out the first item and runs your code, then it pulls out the second item and runs the code again. It repeats that process until it has gone through all of the items.
So in your code you are telling the loop to loop through a list called
loopy, which is invalid since
loopy is the name of the function. And to create a variable called
items, which is problematic since that is an existing variable which holds the list passed to your function.
The list you are meant to loop through is
items and as for the name of the variable that can be anything, but in this case
item seems like the most fitting name.
If you set the loop up in the way I specified like this:
def loopy(items): for item in items: # Loop though `items` and create a variable called `item` print(item) # Print out the item inside the loop
Then your code will pass task 1. Do notice that in addition to changing the names used I also added a colon after the declaration of the loop, which was missing in your code example.