Professor Snape’s Magic Potions

invisibility-potionIn this challenge we will use file handling techniques to read through and access the content of different text files.

We have saved three recipes for the following three magic potions using one text file per recipe:

  • Invisibility Potion:
    A potion that makes you invisible for a short period of time.
  • Aging Potion:
    A potion which causes the drinker to temporarily become older. The more of the potion is drunk, the larger the aging which occurs.
  • Poison Antidote:
    A potion which counteracts ordinary poisons, such as creature bites and stings.

The following Python code opens the text file of the invisibility potion (invisibility-potion.txt) in read-only mode. It then uses a for loop to access and print the content of each line, one line at a time.

Your Challenge

Your challenge consists of adding a menu to this code to let the user decide which potion to display on screen. Based on their choice, the program will open the relevant text file and display the recipe on screen.

Extension Task

Add an extra text file to store this new potion:

Babbling Beverage
A potion that causes uncontrollable speaking of nonsense.

    Add 1 four-leaf clover and 3 dragon scales into a mortar
    Add 1 garlic clove and crush the ingredients
    Pour the ingredients into a copper cauldron
    Add 2 horned slugs in the cauldron
    Add 3 drops of beetroot juice
    Boil and simmer the potion for half an hour
    Stir once every 10 minutes, clockwise

Video Tutorial



The solution for this challenge is available to full members!
Find out how to become a member:
➤ Members' Area

Did you like this challenge?

Click on a star to rate it!

Average rating 4 / 5. Vote count: 40

No votes so far! Be the first to rate this post.

As you found this challenge interesting...

Follow us on social media!

Tagged with: , , ,