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.