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):

  • The purpose of this function is to print the contents of groceries.
    • groceries is a dictionary that contains item name and quantity pairs. Item name is the key and quantity is the value.
  • Create a for-loop that loops through the key-value pairs in the groceries dictionary:
    • .Print the quantity, followed by a tab character "\t", then the item name: quantity item name
  • To test this function and ensure it works, uncomment lines 51 and 52 in shopping.py and run shopping.py.

1. Define a function with the following header: addGroceries(groceries, itemsToAdd):

  • The purpose of this function is to add the items in itemsToAdd to groceries.
    • groceries is a dictionary that contains item name and quantity pairs.
    • itemsToAdd is a list of item names to add to groceries.
    • If an item from itemsToAdd already exists in groceries, then increment its quantity by 1, otherwise add that item to groceries with the quantity 1.
  • To add the items from itemsToAdd to the groceries dictionary, here's a rough algorithm to follow:
    • Create a for-loop that loops through each item in itemsToAdd list: .If the item exists in groceries, increment the quantity (value) by 1, otherwise, add it to groceries with the quantity 1 because it doesn't exist.
    • To test this function, uncomment lines 57, 60, and 61 in shopping.py and run it.

2. Define a function with the following header: groceryTotal(groceries, prices):

  • The purpose of this function is to calculate the total cost of all the groceries and return that value.
    • groceries is a dictionary that contains item name and quantity pairs. Item name is the key and quantity is the value.
    • prices is a dictionary that contains item name and price pairs. Item name is the key and price is the value.
  • To calculate the total, here's a rough algorithm to follow:
    • Create a variable named total and initialize it to 0.
    • Create a for-loop that loops through the key-value pairs in the groceries dictionary:
      • Multiply the quantity by the price of the item, then add the result on to the total variable.
        • The quantity of the item should already be available as a loop variable.
        • To get the price of the item, use the prices dictionary with the item name.
      • Be careful not to overwrite the total variable. You want to add on to what is already there to create a running total.
    • After the loop, return the total variable.
  • To test this function, uncomment lines 66 and 67 in shopping.py and run it.

Upon completing all 3 functions, when running shopping.py you should get the full output shown below.

Example Output

> 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!
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.