RPG inventory

RPG inventory

Grab your sword and shield.

Try


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:

Knowledge Organiser

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.

Command summary

The new commands used in this program and others that may be useful. Select them below to learn more:

x.append(y)

Adds y to the end of list x.

x.insert(y)

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.

x.remove(y)

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

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

x is assigned the index of where string z is in list y.

x.sort()

Sorts all the items in list x into ascending order using a Tim Sort.

x.reverse()

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 (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.

random.shuffle(x)

Randomises the elements of list x. Requires the random library to access this command.

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’]

Investigate


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

Item question

Identify a list in the program.

REVEAL ANSWER

`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.

REVEAL ANSWER

“shield” because indexes start at 0 in lists.

Make


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.

Help

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.

Evaluate


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.

Craig'n'Dave logo

Craig ‘n’ Dave

Bett Awards 2024 Finalist