Drop hier links of afbeeldingen om ze aan de editor toe te voegen.

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.

Feud

Make

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.

Success Criteria

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:

  1. Prompts the user to enter “f”, “b”, “c” or “q” to forage, brew, cast a spell or quit.
  2. The user cannot continue unless they enter a valid input.
  3. If the user enters “q” it returns 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:

  1. Prompts the user to enter the herb they want to look for.
  2. If the herb is a valid herb in the list of herbs it is added to the herbs collected.
  3. It outputs “[herb] found” or “Cannot find herb.”.

Complete the subprogram called brew_spell that:

  1. Prompts the user to enter the spell they want to brew.
  2. For a valid spell it calls the 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.
  3. If the spell is not valid it outputs, “That spell is not in the spell book.”

Complete the subprogram called cauldron that:

  1. Takes two parameters: spell and herb.
  2. If the herbs have been collected…
  3. …the spell is added to the spells brewed list…
  4. …and all herbs used are removed from the herbs collected.
  5. It outputs, “[spell] has been brewed” or “Cannot brew the spell, you don’t have the correct herbs.”

Complete the subprogram called cast_spell that:

  1. Prompts the user to enter the spell they want to cast.
  2. If the spell has been brewed it is removed from the spells brewed.
  3. It outputs, “[spell] has been cast.” or “You have not brewed that spell.”

Complete the main program so that:

  1. Creates an empty list for the herbs collected.
  2. Creates an empty list for the spells brewed.
  3. Repeatedly calls the 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.
🆘 If you're really stuck, use this Parsons code sorting exercise
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