Skip to content

Monster cards

Keep facts about a set of monsters, make a monster of your own, and look at any monster's card.

Start here: open pygwide. On the home page, choose Tutorials, then Python Toolkit, then Lesson 4: Monster cards.

This lesson takes about an hour. The extensions take about 30 minutes more.

What you will learn

  • How to keep facts under names in a dictionary.
  • How to look up, change and add facts.
  • How to keep dictionaries inside a dictionary.
  • How to loop over a dictionary.

What you need from before

Step 1: One card

A dictionary keeps facts under names, called keys. It goes in curly brackets { }. Each fact is written as a key, a colon, and its value:

card = {"power": 8, "speed": 6, "magic": 3}
print(card["power"])
print(card)

card["power"] looks up the value under the key "power", which is 8. A list finds things by their number. A dictionary finds them by their name.

Step 2: Change and add facts

Use = to change a value, or to add a new key:

card = {"power": 8, "speed": 6, "magic": 3}
card["power"] = 9
card["colour"] = "red"
print(card)

Step 3: A whole set of cards

A dictionary's values can be dictionaries too. Here is a set of monsters, each with its own card. Typing out numbers teaches you nothing, so copy this one with the copy button in its corner. Replace your code with it:

monsters = {
    "Flamewing": {"power": 8, "speed": 6, "magic": 3},
    "Bogbeast": {"power": 6, "speed": 2, "magic": 7},
    "Zapsprout": {"power": 3, "speed": 9, "magic": 5},
    "Grumblestone": {"power": 9, "speed": 1, "magic": 4},
    "Mistwhisper": {"power": 2, "speed": 7, "magic": 9},
}

monsters["Bogbeast"] is Bogbeast's card, and monsters["Bogbeast"]["magic"] is its magic, 7.

Step 4: Show a card

A for loop over a dictionary goes through its keys. Add a function that shows one monster's card:

def show_card(name):
    stats = monsters[name]
    print("+--------------------+")
    print(f"  {name}")
    for stat in stats:
        print(f"  {stat}: {stats[stat]}")
    print("+--------------------+")

Try it by adding show_card("Zapsprout") at the end, and running it. Then take that line away again.

Step 5: Make your own monster

Ask for a new monster's name and stats, and add it to monsters:

print("Make your own monster!")
new_name = input("What is it called? ")
power = int(input("Power, from 1 to 10: "))
speed = int(input("Speed, from 1 to 10: "))
magic = int(input("Magic, from 1 to 10: "))
monsters[new_name] = {"power": power, "speed": speed, "magic": magic}

Step 6: Look at any card

Last of all, list every monster's name, then let the player look at cards until they type quit. in checks whether a key is in the dictionary:

print()
print("Your monster cards:")
for name in monsters:
    print(f" - {name}")

choice = ""
while choice != "quit":
    print()
    choice = input("Which card do you want to see? (or quit) ")
    if choice in monsters:
        show_card(choice)
    elif choice != "quit":
        print("You don't have that card.")

Check your code

# Monster cards

monsters = {
    "Flamewing": {"power": 8, "speed": 6, "magic": 3},
    "Bogbeast": {"power": 6, "speed": 2, "magic": 7},
    "Zapsprout": {"power": 3, "speed": 9, "magic": 5},
    "Grumblestone": {"power": 9, "speed": 1, "magic": 4},
    "Mistwhisper": {"power": 2, "speed": 7, "magic": 9},
}


def show_card(name):
    stats = monsters[name]
    print("+--------------------+")
    print(f"  {name}")
    for stat in stats:
        print(f"  {stat}: {stats[stat]}")
    print("+--------------------+")


print("Make your own monster!")
new_name = input("What is it called? ")
power = int(input("Power, from 1 to 10: "))
speed = int(input("Speed, from 1 to 10: "))
magic = int(input("Magic, from 1 to 10: "))
monsters[new_name] = {"power": power, "speed": speed, "magic": magic}

print()
print("Your monster cards:")
for name in monsters:
    print(f" - {name}")

choice = ""
while choice != "quit":
    print()
    choice = input("Which card do you want to see? (or quit) ")
    if choice in monsters:
        show_card(choice)
    elif choice != "quit":
        print("You don't have that card.")

Extensions

Card battle

Make a game where you and the computer each get a random card. You choose power, speed or magic, and whoever's card has the higher number wins.

list(monsters) makes a list of the monsters' names, so random.choice(list(monsters)) picks a random monster. .remove() takes something out of a list, so the computer can't get the same card as you. not in is the opposite of in.

One way to do it
# Monster card battle
import random

monsters = {
    "Flamewing": {"power": 8, "speed": 6, "magic": 3},
    "Bogbeast": {"power": 6, "speed": 2, "magic": 7},
    "Zapsprout": {"power": 3, "speed": 9, "magic": 5},
    "Grumblestone": {"power": 9, "speed": 1, "magic": 4},
    "Mistwhisper": {"power": 2, "speed": 7, "magic": 9},
}

names = list(monsters)
mine = random.choice(names)
names.remove(mine)
theirs = random.choice(names)
print(f"Your card is {mine}: {monsters[mine]}")

stat = ""
while stat not in ["power", "speed", "magic"]:
    stat = input("Choose power, speed or magic: ").lower()

print(f"The computer's card is {theirs}: {monsters[theirs]}")
my_number = monsters[mine][stat]
their_number = monsters[theirs][stat]
print(f"Your {stat} is {my_number}. Theirs is {their_number}.")

if my_number > their_number:
    print("You win!")
elif my_number < their_number:
    print("The computer wins!")
else:
    print("It's a draw!")

More stats

Give every monster another stat, such as "size" or "scariness". Does your show_card() function need to change?

Extra bits

Did you know?

Dictionaries are named after the books. In a real dictionary, you look up a word to find its meaning. In Python, you look up a key to find its value.

Export your project

pygwide keeps your work in this web browser, but club computers can forget it. Press ⇩ Export to save your project as a zip file. Keep it somewhere safe, such as a USB stick or your school drive.

Next time, choose ⇪ Import a project on the home page to carry on with it.