Manage a list of items for when you go shopping.
Watch this video to learn about the new concepts shown in the program:
The new commands used in this program and others that may be useful. Select them below to learn more:
try, except, else, finallyThe indented commands will be attempted, but if they cause an error/exception the program will not crash.
It is possible to nest more than one try command.
except:
Indented commands will be executed if an error occurred. It is good practice to specify the error being trapped. E.g.
except FileNotFoundError:
else:
indented code executes if no error occurred.
finally:
Indented code executes regardless of whether an error occurred or not.
Questions to think about with this program to check your understanding:
Why is the try, except block of code in lines 15-19 a good idea to include in all programs using files?
try:
file = open("shopping.txt", "r")
except FileNotFoundError:
file = open("shopping.txt", "w")
file.close()
The file can be accessed outside of the program and therefore it might have been moved, renamed or deleted by the user, or maybe it may never have been created. The program would crash if it cannot find a file it is expecting to read from. We should stop our program from crashing when exceptions happen. This is called “exception handling”.
Why can’t you sort data directly in a file without reading the contents into memory first?
A file pointer (sometimes called a pipe) can only move forward through a file, it cannot go back once a line of data has been read. That makes it impossible to move an item in a file using the approach taken in this program.
This is why text files are only used for simple data storage. More advanced techniques exist for handling large data sets.
Change the program so that it:
1. Add item
2. Output shopping list
3. Sort list
4. Clear list
5. Quit
Enter choice: 1
Enter the item to add to the list: Bread
1. Add item
2. Output shopping list
3. Sort list
4. Clear list
5. Quit
Enter choice: 4
1. Add item
2. Output shopping list
3. Sort list
4. Clear list
5. Quit
Enter choice: 1
Enter the item to add to the list: Bread
1. Add item
2. Output shopping list
3. Sort list
4. Clear list
5. Quit
Enter choice: 1
Enter the item to add to the list: Bread
1. Add item
2. Output shopping list
3. Sort list
4. Clear list
5. Quit
Enter choice: 1
Enter the item to add to the list: Milk
1. Add item
2. Output shopping list
3. Sort list
4. Clear list
5. Quit
Enter choice: 1
Enter the item to add to the list: Bread
1. Add item
2. Output shopping list
3. Sort list
4. Clear list
5. Quit
Enter choice: 1
Enter the item to add to the list: Milk
1. Add item
2. Output shopping list
3. Sort list
4. Clear list
5. Quit
Enter choice: 2
-----------------------------
Bread
Milk
-----------------------------
1. Add item
2. Output shopping list
3. Sort list
4. Clear list
5. Quit
Enter choice: 1
Enter the item to add to the list: Butter
1. Add item
2. Output shopping list
3. Sort list
4. Clear list
5. Quit
Enter choice: 3
1. Add item
2. Output shopping list
3. Sort list
4. Clear list
5. Quit
Enter choice: 2
-----------------------------
Bread
Butter
Milk
-----------------------------
1. Add item
2. Output shopping list
3. Sort list
4. Clear list
5. Quit
Enter choice: 5
Restricted automated feedback
Automated feedback for this assignment is still under construction. Submitted programs are checked for syntax errors and their source code is checked for potential errors, bugs, stylistic issues, and suspicious constructs. However, no checks are performed yet to see if the program correctly implements the behaviour specified in the assignment.
add_item
# Add an item to the shopping list
def add_item():
---
# Check the file exists
try:
---
file = open("shopping.txt", "r")
---
# Create a file if not
except FileNotFoundError:
---
file = open("shopping.txt", "w")
---
file.close()
---
# Process the file if it existed
else:
---
new_item = input("Enter the item to add to the list: ")
item_exists = False
---
# Read each item from the file
for item in file:
---
item = item.strip()
---
# If it is an item to be added, note this
if item == new_item:
---
item_exists = True
---
file.close()
---
# Only append the new item if it was not found in the file
if not item_exists:
---
file = open("shopping.txt", "a")
---
new_item = new_item + "\n"
---
file.write(new_item)
---
file.close()
output_items
# Output the shopping list
def output_items():
---
# Check the file exists
try:
---
file = open("shopping.txt", "r")
---
# Create a file if not
except FileNotFoundError:
---
file = open("shopping.txt", "w")
---
file.close()
---
# Process the file if it existed
else:
---
print("-----------------------------")
# Read every item in the file and output it
for item in file:
---
item = item.strip()
---
print(item)
---
file.close()
---
print("-----------------------------")
sort_items
# Sort the items by reading them into memory and recreating the file
def sort_items():
---
# Check the file exists
try:
---
file = open("shopping.txt", "r")
---
# Create a file if not
except FileNotFoundError:
---
file = open("shopping.txt", "w")
---
file.close()
---
# Process the file if it existed
else:
---
item_list = []
---
# Read every item in the file and add it to a list in memory
for item in file:
---
item = item.strip()
---
item_list.append(item)
---
file.close()
---
item_list.sort()
---
file = open("shopping.txt", "w")
# Output the list to the file, overwriting previous data
for item in item_list:
---
item = item + "\n"
---
file.write(item)
---
file.close()
clear_list
# Clear the list by writing a new blank file
def clear_list():
---
file = open("shopping.txt", "w")
---
file.close()
menu
# Menu choice
def menu():
---
choice = ""
---
# Continue until quit chosen
while choice != "5":
---
print("1. Add item")
---
print("2. Output shopping list")
---
print("3. Sort list")
---
print("4. Clear list")
---
print("5. Quit")
---
# Validation
while choice not in ["1", "2", "3", "4", "5"]:
---
choice = input("Enter choice: ")
---
# Perform function
match choice:
---
case "1":
---
add_item()
---
choice = ""
---
case "2":
---
output_items()
---
choice = ""
---
case "3":
---
sort_items()
---
choice = ""
---
case "4":
---
clear_list()
---
choice = ""