Recording a narrative.
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:
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.
Questions to think about with this program to check your understanding:
What is the purpose of the squared brackets inside the squared brackets in line 25?
book = [[]]
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]]
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?
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.
Change the program so that it:
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'.
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.
Run your code to check that your program has met the success criteria and produces the required output.
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'.