Use OpenAI to generate choose your own adventure stories in Python code

Christopher Collins
Curious About Python
5 min readNov 6, 2022

--

A choose your own adventure story is a book in which the reader gets to choose what happens next in the story. With Python you can write a choose your own adventure story by using the input() function to ask the reader what they want to do next and then using the if statement to give them different options depending on what they choose.

For example:

print(“You are walking through the forest when you see a big, scary monster.”)choice = input(“What do you want to do? Run away or fight the monster?”)if choice == “run away”:
print(“You run away as fast as you can and the monster doesn’t catch you.”)
elif choice == “fight the monster”:
print(“You fight the monster and win!”)

else:
print(“I don’t understand what you want to do.”)

It takes a bit of time to map all of this out first as a story and then creating the correct Python code for each option. However OpenAI can make creating such stories and running them in Python very simple.

Create your choose your own adventure story line option structure

First you need some text for your story where you have mapped out your options:

--

--

Christopher Collins
Curious About Python

I write about coding, crypto, the tech future,please follow my publication https://medium.com/aiwriters/ 😀