Making a purchase - Why does this work?


#1


I was able to figure out the solution through trial and error, but I'm still more or less stuck in that I don't understand what exactly is happening here and why this code actually works.

If I had to pinpoint where I'm most confused, it would be with:
1. the argument "food" as part of the compute_bill function. It's basically just a placeholder - is that correct? I could call it "Bob" or "xyzabc" and it would serve the same logical function. Yes? No?
2. Syntax question: in the compute_bill function, is food defined as a list by the presence of the for loop?
3. What is going on with prices[item]?


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 item in food:
        total = total + prices[item]
    return total


#2
  1. the argument "food" as part of the compute_bill function. It's basically just a placeholder - is that correct? I could call it "Bob" or "xyzabc" and it would serve the same logical function. Yes? No?

Yes, a placeholder. Now design of function decides what type of data it takes as input.
"Bob" / "xyzabc" are strings not identifiers(variables), They are values.

2.Syntax question: in the compute_bill function, is food defined as a list by the presence of the for loop?

As I said design of function decides what type of data it takes as input. Here food is list of strings(fruits name), but it can also work for tuple etc too. (In reality no one knows what type of input user will pass, in more realistic programs we make our program less user dependent so we deal this part easily)

3.What is going on with prices[item]?

This is a good question, prices is a global variable, means it can be accessed from any part of program, We're using prices dictionary to find price-value of a fruit.


#3

I'm still confused. The argument food only comes into existence when the compute_bill function is declared. And then I write a line of code and declare a variable called item as part of a looping action through food. So food is now defined as a list, or as something that a for loop can iterate through. But prior to that line of code, food is just a placeholder and doesn't have any value, correct?

Now, going down to the next line of code, the one with prices[item] in it. I understood that prices is a global variable. What I don't get is the particular syntax and the path of logic. It seems like what I'm saying here with this code is something like this:

for item in food: #iterate through a list
total = total + prices[item] #the list is called prices. Add the value of each list item to the total variable.

Even as I write this out it seems very confusing and non-intuitive. But maybe I just need repetition to train my brain.


#4

Yes, its a placeholder and it only gets occupied with a value when we call function (and pass value to it, arguments!)
Loops do gives an idea about data type there i.e it's an sequential/iterative data type.
item is an implicit iterator defined on the fly while looping, as it takes one item from our food (at every iteration), the item can be passed to prices to get corresponding value which is nothing but price of fruit?


#5

compras = ["banano","naranja","manzana"]

inventario = {"banano": 6,
    "manzana": 0,
    "naranja": 32,
    "pera": 15
    }
    
precios = {"banano": 4,
    "manzana": 2,
    "naranja": 1.5,
    "pera": 3
    }

def calcularFactura(comida): 
    total = 0 
    for item in comida: 
        total = total + precios[item]
        print total
    return total
    
calcularFactura(["banano","naranja","manzana"])

    for item in comida:

Here I understunt, "comida" make posible we culd enter strings for the loop

calcularFactura(["banano","naranja","manzana"])

But when I call the function, I need to call`it like this, because when i try to call:
calcularFactura(compras[]) they bring me a error.
My question is, can i call a list in a funtion, with out call singles strings?

and other question, how can i make the function give me the final result only (in this case 7.5)?


#6

Hello @arque,
Lists can't be called. (List is a value)
We call function(s) or method(s) (functions defined under some class) using ().
Try doing this..

calcularFactura(compras)

The [] operator is used to slice or access individual items/character of a sequential data type, List is a sequential data type.


#7

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.