Grab your sword and shield.
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:
Lists are useful when you want to have multiple variables with the same identifier. E.g. instead of using three variables: inevntory1, inventory2, inventory3, we would use one list: inventory[0], inventory[1], inventory[3] etc.
This is because the index inside the square brackets can be replaced with a variable. E.g. inventory[choice]. You cannot do this with variables.
Lists are dynamic which means the number of indexes can grow and shrink as needed when a program is running.
x = []
Defines x as a new empty list.
x = ["Craig", "Dave"]
Defines x as a list containing two values: the string "Craig" in index 0 and the string "Dave" in index 1.
The new commands used in this program and others that may be useful. Select them below to learn more:
Adds y to the end of list x.
Inserts z at index y into list x. All other elements are moved down by one index.
Returns True if x is in list y or False if not.
Removes the first occurrence of y from list x. Remember that this will reduce the index of all other elements stored after y by -1.
Removes the first occurrence of y from list x. Remember that this will reduce the index of all other elements stored after y by -1.
x is assigned the index of where string z is in list y.
Sorts all the items in list x into ascending order using a Tim Sort.
Reverses the order of all items in list x.
x is assigned to be a copy of list y. You cannot use `x = y` with lists because x will become a reference (pointer) to y. I.e. x and y are still the same list, but with two different identifiers. Unlike variables, you must copy a list for it to become a copy.
x is assigned a random element from list y. Requires the random library to access this command.
Randomises the elements of list x. Requires the random library to access this command.
x is assigned the number of items in list y. Can also be used to return the number of characters in string y.
x is assigned the value stored in list y at index z. Remember that indexes start at 0 so y[0] is the first item in the list y. Be careful that z is a valid index and not out of bounds of the list.
Can also be used to output the contents of a list in the format: ['value 0', 'value 1', 'value 2']
Questions to think about with this program to check your understanding:
Identify a list in the program.
`inventory` is a list. It can store more than one value with a single identifier. Each value is assigned an index.
State what is stored in index 1 in the data structure assigned in line 55.
"shield" because indexes start at 0 in lists.
Change the program so that it:
Assuming ['sword', 'shield']
You have 2 items in your inventory.
What action do you want to take? (add/craft/look/drop/search) : craft
What item do you want to craft? : charm
You do not have the items to do this.
Assuming ['sword', 'shield', 'gem', 'leather']
You have 4 items in your inventory.
What action do you want to take? (add/craft/look/drop/search) : craft
What item do you want to craft? : charm
charm crafted.
Assuming ['sword', 'shield', 'charm']
You have 3 items in your inventory.
What action do you want to take? (add/craft/look/drop/search) : look
Which inventory slot do you want to look at? :2
You have a charm in slot 2.
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.
Check that charm cannot be crafted.
Add gem to the inventory.
Add leather to the inventory.
Craft charm.
Check that gem and leather are no longer in the inventory.
Check that charm has been added to the inventory.