A simple notebook.

Notebook

Try

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

Knowledge organiser

x = [y for index in range(z)]

Declares a list x of z elements, assigning each element the value y.

Note that any variables or arrays/lists declared inside a sub program have a local scope.

Local scope data structures can only be accessed inside their subprogram.

Any variables or arrays/lists declared outside of a subprogram have a global scope.

Global scope data structures can be accessed anywhere in the program including inside subprograms.

In some situations, assigning an array/list inside a subprogram will create a new local scope structure instead. To ensure you are always using a global scope, the keyword global is used. E.g.

def clear_notes():
    global notebook
    notebook = ["" for note in range(10)]

Global scope data structures should be avoided wherever possible because they can lead to conflicts in the code, especially when more than one person is working on a program and chooses to use the same identifier. Local scope data structures also allow for dynamic use of the memory. That means a program only uses the memory it needs when it needs it. Passing parameters into subprograms and returning values from them is considered a much better practice than using global scope.

Investigate

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

Relation question

Line 60 is used to assign all the notebook enties to an empty string. Why can’t lines 28 and 29 be replaced with this statement instead?

I.e.

notebook = ["" for note in range(10)]

Instead of:

for index in range(len(notebook)):
    notebook[index] = ""
Reveal answer

Notebook is a global scope data structure. Using this command inside a subprogram will declare a new local scope notebook instead of applying the change to the existing global notebook.

Approach question

Consider the show_notebook procedure. Why has an iteration by index been chosen instead of iteration by element?

I.e.

for index in range(len(notebook)):

Instead of:

for note in notebook:
Reveal answer

The index is needed in the output as well as the data itself. An alternative approach might be:

def show_notebook():
    index = 0
    for note in notebook:
        print(index, ":", note)
        index = index + 1

In programming there are often many different ways of approaching the same problem. If the speed of execution and the memory usage is not affected, then the readability of the source code probably matters most. Programmers should aim to create easy to read code wherever possible because it makes debugging easier.

Make

Change the program so that it:

  1. Includes a delete_note function that removes a single note.
  2. Includes a move_note function that swaps two notes.

Typical inputs and outputs from the program would be:

0 : 
1 : 
2 : 
3 : 
4 : 
5 : 
6 : 
7 : 
8 : 
9 : 

a. Add note
d. Delete note
c. Clear notebook
m. Move note
o. Order notes
:a
Note number: 6
Enter the note: Hello
0 : 
1 : 
2 : 
3 : 
4 : 
5 : 
6 : Hello
7 : 
8 : 
9 : 

a. Add note
d. Delete note
c. Clear notebook
m. Move note
o. Order notes
:a
Note number: 3
Enter the note: World
0 : 
1 : 
2 : 
3 : World
4 : 
5 : 
6 : Hello
7 : 
8 : 
9 : 

a. Add note
d. Delete note
c. Clear notebook
m. Move note
o. Order notes
:a
Note number: 8
Enter the note: Loops reduce the amount of code.
0 : 
1 : 
2 : 
3 : World
4 : 
5 : 
6 : Hello
7 : 
8 : Loops reduce the amount of code.
9 : 

a. Add note
d. Delete note
c. Clear notebook
m. Move note
o. Order notes
:m
Note number: 3
To be moved to...
Note number: 6
0 : 
1 : 
2 : 
3 : Hello
4 : 
5 : 
6 : World
7 : 
8 : Loops reduce the amount of code.
9 : 

a. Add note
d. Delete note
c. Clear notebook
m. Move note
o. Order notes
:m
Note number: 3
To be moved to...
Note number: 5
0 : 
1 : 
2 : 
3 : 
4 : 
5 : Hello
6 : World
7 : 
8 : Loops reduce the amount of code.
9 : 

a. Add note
d. Delete note
c. Clear notebook
m. Move note
o. Order notes
:d
Note number: 8
0 : 
1 : 
2 : 
3 : 
4 : 
5 : Hello
6 : World
7 : 
8 : 
9 : 

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.

🆘 If you're really stuck, use this Parsons code sorting exercise
get_note_number
# Function to return valid input
def get_note_number():
---
    valid_input = False
---
    # Validation
    while not valid_input:
---
        note_number = int(input("Note number: "))
---
        # Note must be between 0 and 9.
        if note_number >= 0 and note_number < 10:
---
            valid_input = True
---
        else:
---
            print("Invalid input. Enter 0-9.")
---
    return note_number
add_note, clear_notes
# Procedure to write a new note
def add_note():
---
    index = get_note_number()
---
    note = input("Enter the note: ")
---
    notebook[index] = note
---


# Clear notebook
def clear_notes():
---
    choice = ""
---
    # Validation
    while choice not in ["y", "n"]:
---
        choice = input("Are you sure you want to delete all notes? y/n :")
---
    # if yes selected, clear the notebook
    if choice == "y":
---
        for index in range(len(notebook)):
---
            notebook[index] = ""
delete_note, move_note
# Procedure to clear a note
def delete_note():
---
    index = get_note_number()
---
    notebook[index] = ""
---


# Procedure to swap two notes
def move_note():
---
    first_note = get_note_number()
---
    print("To be moved to...")
---
    second_note = get_note_number()
---
    temp = notebook[first_note]
---
    notebook[first_note] = notebook[second_note]
---
    notebook[second_note] = temp
order_notes, show_notebook
# Procedure to order the notes alphabetically
def order_notes():
---
    notebook.sort()
---


# Procedure to output contents of notebook
def show_notebook():
---
    # Output every note with its index
    for index in range(len(notebook)):
---
        print(index, ":", notebook[index])
menu
# Menu choices
def menu():
---
    print()
    print("a. Add note")
---
    print("d. Delete note")
---
    print("c. Clear notebook")
---
    print("m. Move note")
---
    print("o. Order notes")
---
    choice = input(":")
---
    
    # Process user choice
    match choice:
---
        case "a":
---
            add_note()
---
        case "d":
---
            delete_note()
---
        case "c":
---
            clear_notes()
---
        case "m":
---
            move_note()
---
        case "o":
---
            order_notes()