This lab is about using lists and functions to create a program that can give the total price of groceries and update the stock when needed. The first part is used to remind the student about the usage of the for loop and how it can work with the list functions. After that, it’s time to start building what would seem like a list of grocery goods. Prices and stocks are set, now the total function must be made to calculate how much money would be gained if all the goods were sold.
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 = total + prices[item]
stock[item] = stock[item] – 1
return total
Code used to make the program for Lab.