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 trial

Python Python Basics (2015) Shopping List App Break

Oscar Villarruel
Oscar Villarruel
1,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
506 Points

You need to loop through the argument passed into your function (items). You're trying to loop through the name of your function. Functions are not iterable. In-fact, the error you will likely get if you run your code is:

TypeError: 'function' object is not iterable

Try this:

def loopy(items):
    for item in items:

Also note, you were missing the colon in your for loop.

1 Answer

28,558 Points

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 in keyword.

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.