Grab your sword and shield.

RPG inventory

Try

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: inventory1, inventory2, inventory3, we would use one list: inventory[0], inventory[1], inventory[2] 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:

x.append(y)

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.

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.

x = random.choice(y)

x is assigned a random element from list y. Requires the random library to access this command.

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

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:

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.

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
choose_action, main program
# Procedure to choose an action
def choose_action():
---
    print("You have", len(inventory), "items in your inventory.")
    action = input("What action do you want to take? (add/craft/look/drop/search) : ")
---
    match action:
---
        case "add":
---
            add_item()
---
        case "craft":
---
            craft_item()
---
        case "drop":
---
            drop_item()
---
        case "look":
---
            look()
---
        case "search":
---
            search()            
---
    print()
---


# -------------------------
# Main program
# -------------------------
---
inventory = ["sword", "shield"]
---
while True:
---
    print(inventory)
    choose_action()
add_item, search, drop_item
# Procedure to add an item to the inventory
def add_item():
---
    item = input("What item are you adding to your inventory? : ")
---
    inventory.append(item)
    print("Item added.")
---


# Procedure to search for an item in the inventory
def search():
---
    item = input("What item do you want to see if you have? : ")
---
    if item in inventory:
---
        print("You have the item.")
---
    else:
---
        print("You do not have a", item)
---


# Procedure to remove an item from the inventory
def drop_item():
---
    item = input("What item do you want to drop? : ")
---
    # Check item exists
    if item in inventory:
---
        inventory.remove(item)
        print("Item dropped.")
---
    else:
---
        print("You do not have this item.")
look, craft
# Procedure to show an item in an inventory slot
def look():
---
    slot = int(input("Which inventory slot do you want to look at? :"))
---
    if slot < len(inventory):
---
        print("You have a", inventory[slot], "in slot")
---
    else:
---
        print("Invalid slot.")
---


# Procedure to craft an item
def craft_item():
---
    item = input("What item do you want to craft? : ")
---
    # Craft the item if the correct items are available
    if item == "charm" and "gem" in inventory and "leather" in inventory:
---
        inventory.remove("gem")
        inventory.remove("leather")
        inventory.append("charm")
        print(item, "crafted.")
---
    else:
---
        print("You do not have the items to do this.")