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 Introducing Lists Build an Application Display the List

Why does 'add_to_list' function take an argument of 'item' but 'show_list' doesn't?

Both functions are using item, one is adding the item to the list, the other is printing the item. They both need item, so why isn't item passed to 'show_list'

2 Answers

Jonathan Grieve
MOD
Jonathan Grieve
Treehouse Moderator 91,253 Points

It's hard to say too much without the code you're trying, but it sounds like you're asking about variables passed to function arguments.

Just glancing at the video I think what's happening is show_list(): is using list object that is in the global scope so that is the variable it is using. And the data that is appended and used in another function. In show list, item` is a tracking variable that displays each value as it is looped over.

Remember that variables passed as parameters to a function are placeholders for information passed in a function call.

As Johnathan pointed out, Craig is showing the list by iterating through a global variable -- meaning it can be seen and used by other functions and code within a program. So as you are adding tiems to the global list, you are continuously building it up so that it can be used later.

I actually did it a bit different, but it still acheives the same result. I think my solution is a not as efficient as it uses more code to acheive the same thing, which I think it typically frowned upon with programming. But I hope this helps you understand.

Here is my solution:

def show_list(list):
    for item in list.copy():
        print("* {}".format(item))

 show_list(shopping_list)