Combine everything you have learned to build a text-based choice game.
What if you could build an entire universe where every choice matters, using nothing but words and logic? From 'Zork' to modern RPGs, every great adventure starts with a single line of code.
Before your hero takes their first step, your program needs a way to remember who they are and what they have. We use Variables to track the 'state' of the game. For example, `player_health = 100` or `has_magic_key = False`. These variables act like the game's memory. To make the game interactive, we use the `input()` function to capture player decisions. Think of your code as a conversation: the computer asks a question, the player provides a string, and the variable stores that answer for later use. Without variables, your game would 'forget' everything the player did the moment they moved to the next screen.
1. Create a variable to store the player's name: `name = input('What is your name, traveler? ')` 2. Initialize a starting item: `inventory = 'Wooden Shield'` 3. Print a welcome message using the variable: `print('Welcome, ' + name + '! You carry a ' + inventory + '.')`
Quick Check
If you want to track whether a player has found a secret map, what data type should you use for that variable?
Answer
A Boolean (True or False).
The heart of a text adventure is the Conditional Statement. Using `if`, `elif`, and `else`, you create branches in your story. If a player types 'left', they might find a dragon; if they type 'right', they might find a treasure chest. It is helpful to visualize this as a tree: the trunk is your start, and every choice is a new branch. To keep your code clean, ensure your logic covers all possibilities. If a player types 'jump' when you only offered 'left' or 'right', your `else` statement should act as a safety net to tell them, 'I don't understand that command.'
1. Ask for a direction: `choice = input('Do you go Left or Right? ').lower()` 2. Check the first path: `if choice == 'left': print('You fall into a pit!')` 3. Check the second path: `elif choice == 'right': print('You find a golden door.')` 4. Handle errors: `else: print('Invalid choice. The wolves are closing in!')`
Quick Check
Why is it useful to add .lower() to the end of an input() function in a game?
Answer
It ensures that 'Left', 'LEFT', and 'left' are all treated the same by the program.
To make your game truly 'intermediate,' you need to combine conditions. This is called Nested Logic. Imagine a player reaches a locked door. The program must check two things: Did they choose to 'open' the door, AND do they have the key in their inventory? We use the `and` operator or nested `if` statements to handle this. For example, if and , the player wins. This adds depth, as players must backtrack to find items before they can progress through specific branches of your story tree.
1. Set the requirement: `has_key = True` 2. Get player action: `action = input('You see a gate. What do you do? ')` 3. Use a nested condition: `if action == 'open gate':` ` if has_key == True:` ` print('The gate creaks open!')` ` else:` ` print('It is locked. You need a key.')`
Which Python keyword is used to provide a 'catch-all' response when no other conditions are met?
If you want to check if a player has exactly 0 health, which operator should you use?
A 'logical error' in your game will always cause Python to show an error message and stop running.
Review Tomorrow
In 24 hours, try to list the three keywords used for branching logic and explain the difference between = and ==.
Practice Activity
Try this on your own: Draw a flowchart for a 3-room game. For each room, list one variable you need to track (like 'has_torch') and one 'if' statement that uses it.