Now You Code 4: Shopping List

Write a simple shopping list program. Use a Python list as a shopping list. Function definitions have been written for you, so all you need to do is complete the code inside the function.

The main program loop has a menu allowing you to 1) add to the list 2) remove an item from the list or 3) print the list

Example Run

A - Add Item to Shopping List
P - Print Shopping List
R - Remove Item from Shopping List
Q - Quit Shopping List Program
Enter choice: a
Enter Item to Add: eggs
A - Add Item to Shopping List
P - Print Shopping List
R - Remove Item from Shopping List
Q - Quit Shopping List Program
Enter choice: a
Enter Item to Add: eggs
eggs is already in the list!
A - Add Item to Shopping List
P - Print Shopping List
R - Remove Item from Shopping List
Q - Quit Shopping List Program
Enter choice: p
Your List: ['eggs']
A - Add Item to Shopping List
P - Print Shopping List
R - Remove Item from Shopping List
Q - Quit Shopping List Program
Enter choice: r
Enter Item to Remove from List: peas
peas is not in the list!
A - Add Item to Shopping List
P - Print Shopping List
R - Remove Item from Shopping List
Q - Quit Shopping List Program
Enter choice: r
Enter Item to Remove from List: eggs
A - Add Item to Shopping List
P - Print Shopping List
R - Remove Item from Shopping List
Q - Quit Shopping List Program
Enter choice: p
Your List: []
A - Add Item to Shopping List
P - Print Shopping List
R - Remove Item from Shopping List
Q - Quit Shopping List Program
Enter choice: q

Most of the code has been written for you fill in the areas below.


In [ ]:
# TODO: Write Todo lists for
# #1 Add item to list if it does not exist
# #2 Remove item from list when it does not exist

In [ ]:
# Write code here
def print_menu():
    print("A - Add Item to Shopping List")
    print("P - Print Shopping List")
    print("R - Remove Item from Shopping List")
    print("Q - Quit Shopping List Program")
    return

# prompts for a new item using input() then adds it to the shopping list
# only when it does not exist
# input: shopping list
# output: shopping list
def add_item(shopping_list):
    #todo write code here

    return shopping_list

# sorts then prints the shopping list
# input: shopping list
# output: shopping list
def print_list(shopping_list):
    #todo write code here, use a loop and f-strings

    return shopping_list

# prompts for an item then removes it from the shopping list
# only when it exists
# input: shopping list
# output: shopping list
def remove_item(shopping_list):
    #todo write code here

    return shopping_list        


## Main Program Written For You
shopping_list = []
while True:
    print_menu()
    choice = input("Enter choice: ").upper()
    if choice == 'A':
        shopping_list = add_item(shopping_list)
    elif choice == 'P':
        shopping_list = print_list(shopping_list)
    elif choice == 'R':
        # todo call remove_item here
    
    elif choice == "Q":
        break
    else:
        print('ERROR:', choice,'is not a menu option')

Step 3: Questions

  1. When we write functions we usually don't have input() or print() Python functions in them, but in this solution we do. What is the advantage of doing it in this example?

Answer:

  1. Explain a strategy you can employ to save the list to a file and load it back. How would that work?

Answer:

  1. What does the .upper() method function accomplish in the main program?

Answer:

Step 4: Reflection

Reflect upon your experience completing this assignment. This should be a personal narrative, in your own voice, and cite specifics relevant to the activity as to help the grader understand how you arrived at the code you submitted. Things to consider touching upon: Elaborate on the process itself. Did your original problem analysis work as designed? How many iterations did you go through before you arrived at the solution? Where did you struggle along the way and how did you overcome it? What did you learn from completing the assignment? What do you need to work on to get better? What was most valuable and least valuable about this exercise? Do you have any suggestions for improvements?

To make a good reflection, you should journal your thoughts, questions and comments while you complete the exercise.

Keep your response to between 100 and 250 words.

--== Write Your Reflection Below Here ==--


In [ ]:
# RUN THIS CODE CELL TO TURN IN YOUR WORK!
from ist256.submission import Submission
Submission().submit()