RPG inventory

RPG inventory

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:


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

Item question

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.

Structure question

State what is stored in index 1 in the data structure assigned in line 55.


“shield” because indexes start at 0 in lists.


Success Criteria

Change the program so that it:

  1. Includes a new “craft” action that the player can take in the `choose_action` subprogram.
  2. Includes a new `craft` subprogram that:
  3. Asks the user which item they want to craft.
  4. If the item is a “charm” and there is both a “gem” and “leather” in the inventory, these two items are removed and a “charm” is added.
  5. Reports whether the item was crafted or whether the items do not exist in the inventory.

Typical inputs and outputs from the program would be:

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.

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.

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.

Knowledge Organiser

Use these resources to learn about new commands for this level and to help you meet the success criteria.


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.


Adds y to the end of list x.

x.insert(y, z)

Inserts z at index y into list x. All other elements are moved down by one index.

if x in y

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 element at index y from list x. Remember that this reduces the index of other elements stored after y by -1.

x = y.index(z)

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 = y.copy()

x is assigned to be a copy of list y. You cannot use `x = y` with lists because x will become a reference to y. I.e. x and y are still the same list, but with two different identifiers. You must copy a list for it to become a copy.


Randomises the elements of list x.

x = len(y)

x is assigned the number of items in list y. Can also be used to return the number of characters in string y.

x = y[z]

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.

print (x)

Can also be used to output the contents of a list in the format: [‘value 0’, ‘value 1’, ‘value 2’]


Run the unit tests below to check that your program has met the success criteria.

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.

Craig'n'Dave logo

Craig ‘n’ Dave

In partnership with

Mission Encodeable
Bett Awards 2024 Finalist