Shopping List Manager
Exercise Description
Create a shopping list management program that uses Python lists to store and manipulate shopping items. The program should be able to:
- Add new items to the list
- Remove items from the list
- View the current shopping list
- Check if a specific item is on the list
- Sort the list
This exercise will help you understand how Python lists store and manage collections of data, and how to use various list methods to manipulate that data.
Sample Solution
Here's a shopping list management program:
def shopping_list_manager():
# Initialize empty shopping list
shopping_list = []
print("Welcome to the Shopping List Manager!")
print("Commands: Add(a), Remove(r), View(v), Check(c), Sort(s), Empty(e), Quit(q)")
while True:
# Get user command
command = input("\nEnter command: ").lower()
# Process different commands
if command == 'q':
print("Thank you for using the Shopping List Manager! Goodbye!")
break
elif command == 'a':
item = input("Enter item to add: ")
if item:
shopping_list.append(item)
print(f"'{item}' has been added to the shopping list.")
else:
print("Item name cannot be empty.")
elif command == 'r':
if not shopping_list:
print("Shopping list is empty, nothing to remove.")
continue
item = input("Enter item to remove: ")
if item in shopping_list:
shopping_list.remove(item)
print(f"'{item}' has been removed from the shopping list.")
else:
print(f"'{item}' is not in the shopping list.")
elif command == 'v':
if shopping_list:
print("\nCurrent Shopping List:")
for index, item in enumerate(shopping_list, 1):
print(f"{index}. {item}")
else:
print("Shopping list is empty.")
elif command == 'c':
item = input("Enter item to check: ")
if item in shopping_list:
print(f"'{item}' is in the shopping list.")
else:
print(f"'{item}' is not in the shopping list.")
elif command == 's':
if shopping_list:
shopping_list.sort()
print("Shopping list has been sorted alphabetically.")
print("\nSorted Shopping List:")
for index, item in enumerate(shopping_list, 1):
print(f"{index}. {item}")
else:
print("Shopping list is empty, nothing to sort.")
elif command == 'e':
if shopping_list:
confirm = input("Are you sure you want to empty the shopping list? (yes/no): ").lower()
if confirm in ['yes', 'y']:
shopping_list.clear()
print("Shopping list has been emptied.")
else:
print("Shopping list is already empty.")
else:
print("Invalid command. Please use: a, r, v, c, s, e, q")
# Run the shopping list manager
shopping_list_manager()
Discussion
This program demonstrates several operations with Python lists:
Creating a List: Using
shopping_list = []
to create an empty list.Adding Elements: Using the
append()
method to add new items to the end of the list.Removing Elements: Using the
remove()
method to remove specific items from the list.Checking Membership: Using the
in
keyword to check if an item is in the list.Sorting: Using the
sort()
method to sort the list alphabetically.Emptying a List: Using the
clear()
method to remove all items from the list.Traversing a List: Using a
for
loop withenumerate()
to go through the list and display numbered items.
Lists are one of the most commonly used data structures in Python, allowing you to store and manipulate a collection of related items. In real life, this is similar to writing a shopping list on paper and then adding, removing, or checking items.
Extension Challenges
- Add quantity functionality, allowing users to specify how many of each item they need
- Implement categorization, organizing items by category (e.g., groceries, household items)
- Add priority functionality, letting users mark important items
- Implement price estimation, calculating the total cost of the shopping list
- Add save and load functionality, saving the shopping list to a file and loading it later