Shopping list

Shopping list

Manage a list of items for when you go shopping.

Try


Select the button below to open the Python program in a new window. Run the program and read the lines of code to see if you can understand how it works. It will be helpful to arrange your display so that you can have this browser window on one side of the screen and the code on the other.

Watch this video to learn about the new concepts shown in the program:

Knowledge Organiser

Command summary

The new commands used in this program and others that may be useful. Select them below to learn more:

try, except, else, finally

The indented commands will be attempted, but they prevent the program to crashing if they cause an error/exception.


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.

Investigate


Questions to think about with this program to check your understanding:

Relation question

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

REVEAL ANSWER

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”.

Approach question

Why can’t you sort data directly in a file without reading the contents into memory first?

REVEAL ANSWER

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.

Make


Success Criteria

Change the program so that it:

  1. Includes an option to clear the list, removing any items.
  2. Only adds an item to the shopping list that does not already exist in the list.

Typical inputs and outputs from the program would be:

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

Help

Use the flowcharts if you need more support in understanding the steps required to meet the criteria. Use the Parsons code sorting exercise only if you are really stuck.

Evaluate


Run your code to check that your program has met the success criteria and produces the required output.

Craig'n'Dave logo

Craig ‘n’ Dave

Bett Awards 2024 Finalist