Python - 26
Python - 26: Building a Text Adventure Game
Let's combine input, variables, and conditions to build a small text adventure.
Your challenge: Ask the player to make choices and use nested conditions to change what happens next.
Store the player's choices
Each answer from input() is stored in a variable such as choice or action.
Change the story with conditions
Use if, elif, and else to decide which part of the story should happen.
For example: if choice == "left":
Nest one decision inside another
When the player goes right, the program asks another question. That second if statement belongs inside the first decision.
Try it: Add another choice after the player finds the treasure.
Good Python habit: Keep choice words simple and consistent so it is clear what the player should type.
👀 Show Example Solution
print("Welcome to the Adventure!")
player_name = input("What is your name? ")
print("Hello " + player_name)
print("You enter a dark cave.")
choice = input("Go left or right? ")
if choice == "left":
print("You found treasure!")
elif choice == "right":
print("A monster appears!")
action = input("Run or fight? ")
if action == "run":
print("You escaped safely!")
elif action == "fight":
print("You defeated the monster!")
else:
print("The monster watches you carefully.")
else:
print("You stay at the cave entrance.")
print("Game Over!")The story changes depending on the player's answers, and the nested condition creates another decision inside one branch of the adventure.
