Gather herbs to make spells.
★★☆A game allows players to cast spells against each other. A spell must be brewed in a cauldron before it can be cast. Each spell requires two ingredients (herbs) before it can be brewed. Herbs can be foraged in the game world.
Write a program that allows the user to add a herb to a list of herbs foraged. Once foraged, the user can choose two herbs to be combined to brew a spell. The user can also cast a brewed spell. When spells are brewed the herbs used are removed from the list. The outcome of each action is shown to the user. The list of valid herbs and spells is shown below.
Remember to add a comment before a subprogram, selection or iteration statement to explain its purpose.
Spells and herbs are:
| Spell | Herb 1 | Herb 2 |
|---|---|---|
| teleport | dandelion | burdock |
| protect | pipewort | ragwort |
| sprites | snapdragon | toadflax |
Complete the subprogram called take_action that:
False to quit; otherwise it calls the relevant subprogram (forage_herb, brew_spell or cast_spell) and returns True.Complete the subprogram called forage_herb that:
Complete the subprogram called brew_spell that:
cauldron subprogram with the paramters spell and herb. Spell is the spell to be brewed and herb is a list of the herbs required for that spell.Complete the subprogram called cauldron that:
Complete the subprogram called cast_spell that:
Complete the main program so that:
take_action subprogram until it returns False (the user quits).Typical inputs and outputs from the program would be:
Forage herb, brew or cast a spell? f/b/c/q : f
What herb do you want to look for? : dandelion
dandelion found.
Forage herb, brew or cast a spell? f/b/c/q : f
What herb do you want to look for? : burdock
burdock found.
Forage herb, brew or cast a spell? f/b/c/q : b
What spell do you want to brew? : teleport
teleport has been brewed.
Forage herb, brew or cast a spell? f/b/c/q : c
What spell do you want to cast? : teleport
teleport has been cast.
Forage herb, brew or cast a spell? f/b/c/q : f
What herb do you want to look for? : mint
Cannot find herb.
Forage herb, brew or cast a spell? f/b/c/q : b
What spell do you want to brew? : teleport
Cannot brew the spell, you don't have the correct herbs.
Forage herb, brew or cast a spell? f/b/c/q : b
What spell do you want to brew? : appear
That spell is not in the spell book.
Forage herb, brew or cast a spell? f/b/c/q : c
What spell do you want to cast? : shield
You have not brewed that spell.
forage_herb
# Forage for herb
def forage_herb():
---
herb = input("What herb do you want to look for? : ")
---
# Collect the herb if it exists
if herb in ["dandelion", "burdock", "pipewort", "ragwort", "snapdragon", "toadflax"]:
---
herbs_collected.append(herb)
---
print(herb, "found.")
---
else:
---
print("Cannot find herb.")
cauldron
# Turn herbs into spells
def cauldron(spell, herb):
---
# You must have the correct herbs to make the spell
if herb[0] in herbs_collected and herb[1] in herbs_collected:
---
herbs_collected.remove(herb[0])
herbs_collected.remove(herb[1])
---
spells_brewed.append(spell)
---
print(spell, "has been brewed.")
---
else:
---
print("Cannot brew the spell, you don't have the correct herbs.")
brew_spell
# Brew spell with required ingredients
def brew_spell():
---
spell = input("What spell do you want to brew? : ")
---
# If the spell is valid try and brew in the cauldron
match spell:
---
case "teleport":
---
cauldron(spell, ["dandelion", "burdock"])
---
case "protect":
---
cauldron(spell, ["pipewort", "ragwort"])
---
case "sprites":
---
cauldron(spell, ["snapdragon", "toadflax"])
---
case _:
---
print("That spell is not in the spell book.")
take_action
# Player takes an action
def take_action():
---
choice = ""
---
# Get a valid action from the player
while choice not in ["f", "b", "c", "q"]:
---
choice = input("Forage herb, brew or cast a spell? f/b/c/q : ")
---
if choice == "q":
return False
---
# Take the action chosen
match choice:
---
case "f":
---
forage_herb()
---
case "b":
---
brew_spell()
---
case "c":
---
cast_spell()
---
return True
cast_spell, main program
# Cast spell
def cast_spell():
---
spell = input("What spell do you want to cast? : ")
---
# Cast a spell only if it has been brewed
if spell in spells_brewed:
---
spells_brewed.remove(spell)
---
print(spell, "has been cast.")
---
else:
---
print("You have not brewed that spell.")
---
# -------------------------
# Main program
# -------------------------
---
herbs_collected = []
spells_brewed = []
---
# Keep playing until the user quits
while take_action():
pass