Recording a narrative.

Journey log

Try

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

Knowledge organiser

x = [[]]

Declares x as an empty 2D list.

x.append([])

Adds a new row to 2D list x. The new row contains an empty list.

x[y].append(z)

Adds a new element z to row y in the 2D list x.

Investigate

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

Purpose question

What is the purpose of the squared brackets inside the squared brackets in line 25?

book = [[]]
Reveal answer

To define an empty list inside an empty list.

This creates a table of unknown rows and unknown columns. It is easier to visualise like this…

Outside brackets Inside brackets
Chapter 1 Story log 1
  Story log 2
Chapter 2 Story log 3
  Story log 4
  Story log 5

…where the number of chapters and story logs can increase and decrease at any time. You can also visualise it like this:

[[Story log 1, Story log 2], [Story log 3, Story log 4, Story log 5]]
Reason question

Why has the programmer chosen not to define the number of indexes in line 25? I.e. why use a list instead of an array for storing data?

Reveal answer

Lists are dynamic. That means the number of indexes in the structure can grow and shrink as the program is running. With an array the number of indexes are static (fixed) when the program is written and cannot change. Using a list allows any number of chapters and story entries to be added to the book later. This is better for a situation where you may want to expand the story.

Make

Change the program so that it:

  1. Outputs a heading at the beginning of each chapter.
  2. Outputs the story so far to a file called, log.txt, so that modding tools could read the data generated by the program and make use of it.

Typical inputs and outputs from the program would be:

CHAPTER 1
----------

Log 1 : I find myself alone on a strange world, unequipped and in danger. I have no memory of how I got here, no sense of a before.
Log 2 : My Exosuit at least seems to know what it's doing, and I am not dead yet...

CHAPTER 2
----------

Log 3 : I received a set of mysterious coordinates from an unknown source.
Log 4 : I followed the signal, and found the wreckage of an abandoned starship.
Log 5 : There was little to be gained from the wreck, but the distress beacon contained the hailing frequency labelled 'ARTEMIS'.

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
output
# Output the story so far
def output():
---
    log = 1
---
    # Loop through the chapters
    for chapter in range(1, len(book)):
---
        print("CHAPTER", chapter)
        print("----------")
        print()
---
        # Loop through the story entries in the chapter
        for entry in book[chapter]:
---
            print("Log", log, ":", entry)
---
            log = log + 1
---
        print()
save_log
# Save the logbook
def save_log():
---
    log = 1
---
    file = open("log.txt", "w")
---
    # Loop through each chapter starting at chapter 1
    for chapter in range(1, len(book)):
---
        file.write("CHAPTER" + str(chapter) + "\n")
---
        file.write("----------\n")
---
        file.write("\n")
---
        # Loop through the story entries in the chapter
        for entry in book[chapter]:
---
            line = "Log " + str(log) + ": " + entry + "\n"
---
            file.write(line)
---
            log = log + 1
---
        file.write("\n")
---
    file.close()
Main program
# -------------------------
# Main program
# -------------------------
---
book = [[]]
---
add_chapter()
---
add_story(1, "I find myself alone on a strange world, unequipped and in danger. I have no memory of how I got here, no sense of a before.")
add_story(1, "My Exosuit at least seems to know what it's doing, and I am not dead yet...")
---
add_chapter()
---
add_story(2, "I received a set of mysterious coordinates from an unknown source.")
add_story(2, "I followed the signal, and found the wreckage of an abandoned starship.")
add_story(2, "There was little to be gained from the wreck, but the distress beacon contained the hailing frequency labelled 'ARTEMIS'.")
---
output()
save_log()