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 |
take_action that:False to quit; otherwise it calls the relevant subprogram (forage_herb, brew_spell or cast_spell) and returns True.forage_herb that: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.cauldron that:cast_spell that:take_action subprogram until it returns False (the user quits).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