Can someone explain what is going on here?


#1

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:
if stock[item] > 0:
total += prices[item]
stock[item] -= 1
return total

I am using the word “food” for what, a variable name? How does the computer know to loop through stock, the code looks like I am telling the computer to loop through food, yet there is no food list. So the word/variable food is just a place holder? Does the computer just ignore the word food? This is why I always get stuck on learning programming. This little nuanced things, never seem to have logic behind them. How does the word food connect to any dictionary or list above it?


#2

Arguments to a function are provided when the function is called.
It wouldn’t be useful for a function to predefine its input, imagine if + always returned 5 no matter what the input is.

def add(a, b):
    return a + b

#3

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