#!/usr/bin/env python # coding: utf-8 # # Chapter 10 A Day at the Supermarket # # In[1]: # 01 BeFOR We Begin names = ["Adam","Alex","Mariah","Martine","Columbus"] for e in names: print (e) # In[2]: # 02 This is KEY webster = { "Aardvark" : "A star of a popular children's cartoon show.", "Baa" : "The sound a goat makes.", "Carpet": "Goes on the floor.", "Dab": "A small amount." } # Add your code below! for k in webster: print (webster[k]) # In[3]: # 03 Control Flow and Looping a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13] for num in a: if num % 2 == 0: print (num) # In[4]: # 04 Lists plus Functions # Write your function below! def fizz_count(x): count = 0 for e in x: if e == 'fizz': count += 1 return count # In[5]: # 05 String Looping for letter in "Codecademy": print (letter) # Empty lines to make the output pretty print() print() word = "Programming is fun!" for letter in word: # Only print out the letter i if letter == "i": print (letter) # In[6]: # 06 Your Own Store prices = {"banana" : 4, "apple" : 2, "orange" : 1.5, "pear" : 3} # In[7]: # 07 Investing in Stock stock = {"banana" : 6, "apple" : 0, "orange" : 32, "pear" : 15} # In[8]: # 08 Keeping Track of the Produce stock = {"banana" : 6, "apple" : 0, "orange" : 32, "pear" : 15} prices = {"banana" : 4, "apple" : 2, "orange" : 1.5, "pear" : 3} for fruit in stock: print (fruit) print ("price: " + str(prices[fruit])) print ("stock: " + str(stock[fruit])) # In[9]: # 09 Something of Value prices = { "banana": 4, "apple": 2, "orange": 1.5, "pear": 3 } stock = { "banana": 6, "apple": 0, "orange": 32, "pear": 15 } income = 0.0 for fruit in prices: income += prices[fruit] * stock[fruit] print (income) # In[10]: # 10 Shopping at the Market groceries = ["banana", "orange", "apple"] # In[11]: # 11 Making a Purchase 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): costs = 0.0 for item in food: costs += prices[item] return costs # In[12]: # 12 Stocking Out 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): costs = 0.0 for item in food: if stock[item] > 0: stock[item] -= 1 costs += prices[item] return costs print (compute_bill(['apple', 'pear', 'pear', 'orange'])) # In[13]: # 13 Lets Check Out 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): costs = 0.0 for item in food: if stock[item] > 0: stock[item] -= 1 costs += prices[item] return costs