Solution: Add Layers to Your Adventure
Explore how to create a choose-your-own-adventure game in Python by defining a function and using if-elif-else statements. Understand how to add decision layers that affect the game flow based on player choices, and learn to handle unexpected input with default responses.
We'll cover the following...
This program is a simple choose-your-own-adventure game where your choices decide what happens next.
How it works:
def jungle_adventure():defines a function that contains the whole game.The player first chooses a path:
"river","mountain", or"jungle".Depending on the choice, the game runs a different block of code using
if,elif, andelse.Some paths (like river and mountain) include a second decision, where the player picks what to do next — and each choice has its own outcome printed with
print().If the player types something unexpected, the game shows a default message using the
elsepart.
def jungle_adventure():
# First decision: Choose between three paths
path = input("You find three paths: river, mountain, or jungle. Where do you go? ")
if path == "river":
# Second decision with three possible actions
action = input("At the river, do you swim, build a raft, or follow the shore? ")
if action == "swim":
print("You swim with dolphins!")
elif action == "build a raft":
print("You float downstream to a hidden village!")
elif action == "follow the shore":
print("You find a boat tied to a tree and sail away!")
else:
print("You wait too long and it gets dark.")
elif path == "mountain":
# Second decision with three possible outcomes
action = input("At the mountain, do you explore a cave, enjoy the view, or set up camp? ")
if action == "explore a cave":
print("You find glowing crystals and ancient symbols!")
elif action == "enjoy the view":
print("You spot a distant city — your next quest!")
elif action == "set up camp":
print("You build a cozy fire and rest under the stars.")
else:
print("You get cold and decide to head back.")
elif path == "jungle":
print("You get tangled in vines and meet a group of talking monkeys!")
else:
print("You wander in circles and end up where you started.")
jungle_adventure()