Choose Your Own Adventure
Learn how to combine functions, input, and logic into a simple game.
We'll cover the following...
Now that you can build your functions and take input, let’s make something fun: a mini adventure game in which the user chooses the story!
This lesson combines everything we’ve learned: input(), variables, if/else, and functions.
Our first story path
Let’s create a mini game where we write a function that asks the player to make a choice and reacts based on their answer.
We’ll focus on:
How the
input()is used inside the function.How the
if,elif, andelsestatements decide the outcome.
We’re writing a simple story where the user makes one decision, and the story changes based on their answer.
def adventure():
print("You stand at a fork in the road.")
choice = input("Do you go left or right? ")
if choice == "left":
print("You encounter a friendly dragon!")
elif choice == "right":
print("You find a hidden treasure chest!")
else:
print("You get lost in the woods.")
adventure()We just made an interactive story!
Add more depth
Now, let’s make the story a bit longer by adding a second question. This is how you create branching adventures!
def adventure():
print("You stand at a fork in the road.")
choice = input("Do you go left or right? ")
if choice == "left":
print("You encounter a friendly dragon!")
second = input("Do you talk to the dragon or walk away? ")
if second == "talk":
print("The dragon gives you a magical map!")
elif second == "walk away":
print("You safely head back home, a little disappointed.")
else:
print("The dragon flies off while you hesitate.")
elif choice == "right":
print("You find a hidden treasure chest!")
second = input("Do you open it or leave it? ")
if second == "open":
print("You find gold and a mysterious key!")
elif second == "leave":
print("You walk away, wondering what might've been inside.")
else:
print("It disappears while you're thinking.")
else:
print("You get lost in the woods.")
adventure()Now the story has branches! This is what “adding depth” means—your choices now lead to new situations and decisions!
Make it yours
Use your imagination! Change the setting, choices, and outcomes.
Here’s a fun example set in outer space:
def adventure():
print("We're in space. An alien ship approaches.")
response = input("Do we hide or wave at them? ")
if response == "hide":
print("They pass by peacefully.")
elif response == "wave":
print("They beam us aboard and make us their king!")
else:
print("They ignore us. We float alone forever.")
adventure()Your story, your rules!