Lab Assignment #7

A Day at the Supermarket


Description


BeFOR We Begin
Use a for loop to print out all of the elements in the list names.

5-1


This is KEY!
Use a for loop to go through the webster dictionary and print out all of the definitions.

5-2


Control Flow and Looping
1. Like step 2 above, loop through each item in the list called a.
2. Like step 3 above, if the number is even, print it out. You can test if the item % 2 == 0 to help you out.

5-3


Lists + Functions
Write a function that counts how many times the string “fizz” appears in a list.
1. Write a function called fizz_count that takes a list x as input.
2. Create a variable count to hold the ongoing count. Initialize it to zero.
3. for each item in x:, if that item is equal to the string “fizz” then increment the count variable.
4. After the loop, please return the count variable.
For example, fizz_count([“fizz”,”cat”,”fizz”]) should return 2.

5-4


String Looping
Run the code to see string iteration in action!

5-5


Your Own Store!
1. Create a new dictionary called prices using {} format like the example above.
2. Put these values in your prices dictionary, in between the {}:

5-6


Investing in Stock
Create a stock dictionary with the values below.

5-7


Keeping Track of the Produce
1. Loop through each key in prices.
2. Like the example above, for each key, print out the key along with its price and stock information. Print the answer in the following format:
apple
price: 2
stock: 0
Like the example above, because you know that the prices and stock dictionary have the same keys, you can access the stock dictionary while you are looping through prices.
When you’re printing, you can use the syntax from the example above.

5-8


Something of Value
Let’s determine how much money you would make if you sold all of your food.
1. Create a variable called total and set it to zero.
2. Loop through the prices dictionaries.
3. For each key in prices, multiply the number in prices by the number in stock. Print that value into the console and then add it to total.
4. Finally, outside your loop, print total.

5-9


Shopping at the Market
First, make a list called groceries with the values “banana”,”orange”, and “apple”.

5-10


Making a Purchase
1. Define a function compute_bill that takes one argument food as input.
2. In the function, create a variable total with an initial value of zero.
3. For each item in the food list, add the price of that item to total.
4. Finally, return the total.
Ignore whether or not the item you’re billing for is in stock.
Note that your function should work for any food list.

5-11


Stocking Out
Make the following changes to your compute_bill function:
1. While you loop through each item of food, only add the price of the item to total if the item’s stock count is greater than zero.
2. If the item is in stock and after you add the price to the total, subtract one from the item’s stock count.

5-12


Let’s Check Out!
Click Save & Submit Code to finish this course.

5-13


Code
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 i in food:
if stock[i] > 0:
total = total + prices[i]
stock[i] = stock[i] – 1
return total