Download the following file: shopping.py
from LastnameFirstname02 import * # Change LastnameFirstname02 to your own LastnameFirstname02
"""
shopping.py will test the functions displayGroceries, addGroceries, and groceryTotal defined in a separate file.
An initial grocery list is implemented as a dictionary and new items are added to the grocery list.
This file and LastnameFirstname02.py must be in the same folder.
Author: Ed Meyer
"""
# ~~~ Do not modify code beyond this point other than to uncomment the lines
# ~~~ that are related to calling your custom functions.
# Current grocery list, implemented as a dictionary to store the quantities of each item.
groceries = {
'apple': 2,
'banana': 2,
'yogurt': 7,
'milk': 1,
'eggs': 12,
'spam': 1,
'cabbage': 3,
'rice': 1,
'bread': 1,
'chips': 1,
'peanut butter': 1
}
# New items to add to the grocery list.
new_items = ['banana', 'yogurt', 'bread', 'negi', 'peanut butter', 'yogurt',
'yogurt', 'apple', 'banana', 'spam', 'ham', 'chocolate']
# Price list for each grocery item.
prices = {
'apple': 0.50,
'banana': 0.39,
'yogurt': 1.10,
'milk': 3.5,
'eggs': 0.42,
'spam': 3.50,
'cabbage': 0.69,
'rice': 12.00,
'bread': 4.50,
'chips': 4.00,
'peanut butter': 4.00,
'negi': 2.50,
'ham': 4.00,
'chocolate': 2.00
}
# Display the current grocery list before adding the new items.
print("Display current shopping list:")
displayGroceries(groceries)
print()
# Add the new items to the grocery list
addGroceries(groceries, new_items)
# Display the updated grocery list.
print("New shopping list:")
displayGroceries(groceries)
print()
# Display the total amount.
total = groceryTotal(groceries, prices)
print("Total amount is $" + str(total))
print("Program done!")
shopping.py is a driver file that contains code that simulates adding items to a grocery list. There is an initial "grocery list", items will get added to it, then the updated grocery list is redisplayed. The total cost of the groceries is also calculated and displayed.
The "grocery list" is implemented as a dictionary, it contains items and quantities. The keys are the names of the items and the values are the quantities of the item. New items that are being added to the grocery list is implemented as a list. There is a second dictionary called prices that represents the prices for each item.
What you will be doing for this assignment is defining custom functions in a separate file. One function will simply print out the grocery dictionary, another will add items to the groceries dictionary, and a last function will calculate the grocery total. The functions will be imported into the given shopping.py driver file and run. There is already code in the driver file that will call the custom functions, but it is currently commented out so they do not execute. You will need to uncomment the appropriate lines when you define the corresponding function.
Note: There is no user input for this assignment.
Open shopping.py in Atom. At the top of the file, change the filename LastnameFirstname02 in the import to your own last name and first name.
Save shopping.py.
Create a new Python script named LastnameFirstname02.py, where Lastname is your own last name and Firstname is your own first name. Ensure this file is in the same folder as shopping.py. This script will only contain function definitions. Define the following 3 functions given the descriptions below:
0. Define a function with the following header: displayGroceries(groceries):
1. Define a function with the following header: addGroceries(groceries, itemsToAdd):
2. Define a function with the following header: groceryTotal(groceries, prices):
Upon completing all 3 functions, when running shopping.py you should get the full output shown below.
> python shopping.py
Display current shopping list:
2 apple
2 banana
7 yogurt
1 milk
12 eggs
1 spam
3 cabbage
1 rice
1 bread
1 chips
1 peanut butter
New shopping list:
3 apple
4 banana
10 yogurt
1 milk
12 eggs
2 spam
3 cabbage
1 rice
2 bread
1 chips
2 peanut butter
1 negi
1 ham
1 chocolate
Total amount is $73.17
Program done!