Stocking Out: Hows does compute_bill function see shopping_list?


This is working but can someone help a confused newbie out? I don't understand how the compute_bill function "sees" or is referencing shopping_list

shopping_list = ["banana", "orange", "apple"]

stock = {
"banana": 6,
"apple": 0,
"orange": 32,
"pear": 15

prices = {
"banana": 4,
"apple": 2,
"orange": 1.5,
"pear": 3

Write your code below!

def compute_bill(food):
total = 0
for c_bVar in food:
if stock[c_bVar] > 0:
total += prices[c_bVar]
stock[c_bVar] -= 1
return total


Hello, @snarl!
Your function compute_bill() has an argument named food and that same argument will store the reference of your list when you call it :

print compute_bill(shopping_list)

With that, you'll be able to manipulate your list and do whatever the lession asks you to do.

Hope I was clear!

Best regards,


thanks @g4be but I'm still not getting it: I don't see how the "food" argument in the function references the items in the shopping_list . Think I'm just going to go repeat some of the earlier lessons until this sinks in. Appreciate your help!

List and Function. need explanation. thanks

Okay, let me try to explain this way.
Everything in Python is pretty much a reference and a reference is something that if you have, you can manipulate it and do anything, add elements, remove elements, alter elements, etc.
Think like an e-mail. If you know the password of an e-mail, you'll be able to manipulate it and do whatever you want it.
A reference to a list is just like an e-mail password, if you have that you can do many, many things.
In the example above, the argument food will hold the reference of the list shopping_list so either if you work with shopping_list or food, you'll get the same output.
Here's a screenshot of a code I wrote as an example:

Here's the code too:

listA = [1,2,3,4,5]
def exampleFunction(argList):
    listB = [17,65,34,25]
    print "I'm the argument who has the reference of list A: ", argList
    print "I'm the actual list A: ", listA
    print "Okay, now I'll try to print list B :D"
    print listB
    print "What? I can't print list B ?! Aww :("

The first line of our code has a list named listA declared with those values from 1 to 5.
Since we declared that outside any other function, we call this "structure" global and due to that, any function or variable will be able to use it, following the correct instructions when it comes to list manipulation, and do anything you'd like to do, depends on the purpose of your code.
Since our listA is global and we have created it way before calling a function with it will be able to manipulate it inside of that function.

Did you see that either printing the argument argList and listA we got the exact same values? Considering we didn't change anything, of course.

But notice that at line 04 we created another list named listB and we tried to print that out on the outside but it didn't work... why?
The thing is: variables and structures that have been created inside of a function scope will only exist while this function exists and can only be used inside of those same functions.

That means if you try to print listB outside of the function exampleFunction() we would cause an error, the compiler would throw an exception and inform us that:

name 'listB' is not defined

because that exact same was created in another scope.

In that case, if we want to print that out, there are some ways to do it:
1. Use return and return listB from the function exampleFunction()
2. Use another function inside of that same function to get listB and on the other function, return it.

After returning, we would just need to store that return inside of another variable and that same variable will have the reference of listB and guess what? It will be the same idea of the argument food and the list shopping_list.

If I wasn't clear, let me know and I'll explain in a different way, okay?

Best regards,

Edit: check this site if you have more doubts about scopes and things related: A Beginner’s Guide to Python’s Namespaces, Scope Resolution, and the LEGB Rule


Hi g4be,

What if you declared listB globally and tried printing arglist? How does it determine which to select? Or will it always be the first that's delcared, in this instance listA?



@void.exe: If you declared listB globally and wanted to print argList, it would depend on which one of them you pass as argument.
But let's suppose there's no argument and you want to print the values inside of the function, all you'll need to is to use print and choose which list you'd like to print.

Best regards,