Python for Humans · Draft v0.3 · Nov 16, 2026 · Draft position: chapter 3 · Fix the pizza planner from the new Numbers chapter, and treat number conversion as review.
Making Decisions
If & Conditionals
- You'll build
- Fix the pizza party planner, create an adventure with different endings, then play a one-guess number game.
In the last two chapters, we built programs that could ask questions, tell ridiculous stories, and plan a pizza party.
But our programs always followed the same instructions, regardless of what we told them. Our pizza planner even ordered a pizza nobody needed.
What if we could change that? What if Python could choose what to do based on our answers?
Making a Choice
Remember our little robot? Let's ask it a very important question.
# Click Run and answer yes. Now run it again and answer no. # Each Run starts fresh. answer = input("Do you like pizza? (yes/no) ") if answer == "yes": print("Excellent! Pizza is delicious.") else: print("More pizza for me!")
Did you notice what happened? Our robot responded differently depending on what you typed! That's because we've introduced something new: an if statement.
Take another look at this part of our program:
if answer == "yes": print("Excellent! Pizza is delicious.")
The if tells Python to check whether something is true. In this case, we're checking whether the value stored in answer equals "yes". Notice that we're using two equal signs, ==, instead of one.
Previously, we used a single equal sign, =, to assign values to variables. But two equal signs, ==, ask Python whether two values are equal.
If the answer is yes, Python runs the indented line underneath. But what if the answer is no? That's where else comes in:
else: print("More pizza for me!")
The else tells Python what to do when the condition isn't true.
Try it yourself: Run the program above and enter maybe. Which message do you get? Now try Yes with a capital Y.
Python is very literal! It treats "yes" and "Yes" as different strings. And since our program only checks for "yes", every other answer leads to the else message.
Mind the Indentation
There's something else new in our program. Notice how the print() lines are indented underneath if and else. In Python, those spaces matter!
Indentation tells Python which instructions belong together. The indented code below an if runs only when its condition is true. The colon : at the end of the if and else lines tells Python that an indented block of code is coming next.
Removing the indentation produces an error: Python needs an indented block after if.
# Remove the four spaces before the first indented print(). # Click Run, then restore the spaces and run again. # Each Run starts fresh. answer = input("Do you like pizza? (yes/no) ") if answer == "yes": print("Excellent! Pizza is delicious.") else: print("More pizza for me!")
Now add this line at the very end of the program, with no spaces before it:
print("Thanks for answering!")
Before you run it, predict: will this message appear when you answer yes, no, or both? Try both answers to check.
Because the new line isn't indented, it sits outside the if and else blocks. Python runs it after either choice.
More Than Two Choices
Our robot can now choose between two possibilities. But what if we want to give it more options?
Imagine a robot that recommends something to do based on the weather.
# Your turn: Add another kind of weather. What should your robot # recommend when it's windy? Or foggy? Or raining meatballs? # Try running the program with different answers. # Each Run starts fresh. weather = input("What's the weather like? ") if weather == "sunny": print("Let's go to the beach!") elif weather == "rainy": print("Let's stay inside and read.") elif weather == "snowy": print("Time for a snowball fight!") else: print("Sounds like interesting weather!")
We have another new word here: elif. It's short for else if.
Python starts at the top and checks each condition in order. When it finds one that's true, it runs the corresponding instructions and skips the remaining choices.
If none of the conditions match, it runs the else block.
This means our programs can now choose between several possible outcomes!
Fix the Pizza Planner
Now we can fix the bug from the last chapter. Our planner always added one more pizza for the extra slices, even when there weren't any extra slices.
Remember what a remainder of 0 tells you? The slices divide evenly. That's exactly the question our program needs to ask.
# Try 15 slices, then 16, then 8. Each should order the right number # of pizzas. slices_per_pizza = 8 slices_needed = int(input("How many slices do you need? ")) pizzas = slices_needed // slices_per_pizza if slices_needed % slices_per_pizza == 0: print("The slices divide evenly. No extra pizza needed!") else: pizzas = pizzas + 1 print(f"Order {pizzas} pizzas.")
== works with numbers just as it does with strings. When slices_needed % slices_per_pizza is 0, there are no extra slices, so we keep the number of whole pizzas. Otherwise, we add one more.
The line pizzas = pizzas + 1 might look odd at first. Python works out the right side first, the current value of pizzas plus one, then stores the result back in pizzas. It's how you change a number based on its old value, like adding points to a score.
Your turn: Open your Pizza Party Planner from the last chapter and replace its pizzas = slices_needed // slices_per_pizza + 1 line with this fix. Then try 4 people eating 2 slices each again.
Build Your Own Adventure
Now that our programs can make decisions, let's put that new ability to use.
Have you ever played a choose-your-own-adventure game? You read a story, make a decision, and discover what happens next. Different choices lead to different adventures.
We can build one with Python!
Let's start with a mysterious castle.
# Run the program and choose the red door. # Now try the blue door. # Each Run starts fresh. print("Welcome to the Mysterious Castle!") print("You find yourself standing in a dark hallway.") print("Ahead are two doors: one red and one blue.") door = input("Which door do you choose? (red/blue) ") if door == "red": print("You discover a sleeping dragon!") elif door == "blue": print("You find a chest full of treasure!") else: print("You decide to stay in the hallway. Probably safer.")
We have two completely different outcomes depending on what the player chooses. And we accomplished that with just a few lines of Python!
But our adventure is over almost as soon as it begins.
What if we wanted to keep going?
Another Decision
Let's make our adventure a little more interesting.
This time, choosing the red door leads to another decision.
# Each Run starts fresh. print("Welcome to the Mysterious Castle!") print("Ahead are two doors: one red and one blue.") door = input("Which door do you choose? (red/blue) ") if door == "red": print("You discover a sleeping dragon!") action = input("Do you sneak or shout? ") if action == "sneak": print("You tiptoe past the dragon and steal its treasure!") else: print("The dragon wakes up. It looks hungry!") elif door == "blue": print("You find a chest full of treasure!") else: print("You decide to go home instead.")
Now we have a story with several possible outcomes!
Notice that there's an if statement inside another if statement.
This is called a nested conditional. It's just a way of making one decision lead to another.
If the player chooses the red door, we ask another question. But if they choose the blue door, we skip that question entirely.
Look closely at the indentation. The second if is indented inside the first one, and its print() lines are indented even further.
That's how Python knows which instructions belong to which decision.
Try choosing different paths and see what happens.
Your Turn: Create an Adventure Game
You've learned enough to build your own choose-your-own-adventure game.
It could take place in a haunted house, an underwater city, on another planet, or anywhere else you can imagine.
Here's a place to begin:
# Start with a setting, give the player a choice, and use if, elif, # and else to decide what happens next. # Before you run it, write down the answers that should reach each # ending. Try each path and check whether it reaches the ending you # expected. # Create at least two different endings, then add a second decision # inside one path. You can extend the castle instead: give the blue # door its own question, or let the player try to befriend the # dragon. # Each Run starts fresh. print("Welcome to my adventure!") choice = input("Which path will you take? (left/right) ") if choice == "left": print("Something mysterious happens!") else: print("A different adventure begins!")
Don't worry if things don't work the first time. Experiment, make mistakes, and borrow ideas from the examples above.
The story is yours to tell!
Guess My Number
Our adventure reacts to words. Let's make a game that reacts to numbers. The computer has a secret, and you get one guess. It will tell you whether your guess is too high, too low, or just right.
You already know from the Numbers chapter that input() always gives us a string, even when someone types digits. For our game, we need a number we can compare with the secret, so we'll convert the guess with int():
guess = int(input("What's your guess? "))
Remember that if the player types seven or 7.5, int() gives a ValueError. For now, enter whole numbers written as digits. Later, in Try Again, we'll learn how to handle an error and ask the player to try again.
Higher or Lower?
Here are the comparisons our game needs. The symbol < means less than, and > means greater than. Like ==, each comparison gives us True or False.
Those are Python's two answers to a yes-or-no question. Both start with a capital letter. An if runs its block when the comparison gives True.
# Predict the three answers, then run it. Change guess to 9, then to # 7, and check again. # Each Run starts fresh. secret = 7 guess = 3 print(guess < secret) print(guess > secret) print(guess == secret)
Make Your First Guess
# Run this starter. It already asks for a guess and turns the answer # into a number. # Now replace the final print() with three decisions: # 1. If guess < secret, display Too low!. # 2. Otherwise, if guess > secret, display Too high!. # 3. Otherwise, display You got it!. # Borrow the if, elif, and else shape from the weather robot. Check # three separate runs: enter 3, then 9, then 7. Each should get a # different response. # Each Run starts fresh. secret = 7 print("I'm thinking of a number from 1 to 10.") guess = int(input("What's your guess? ")) print("You guessed", guess)
Here's a worked version to compare after you've tried:
# Each Run starts fresh. secret = 7 print("I'm thinking of a number from 1 to 10.") guess = int(input("What's your guess? ")) if guess < secret: print("Too low!") elif guess > secret: print("Too high!") else: print("You got it!")
The question runs once, and exactly one response runs. Wrong guess? For now, click Run to try again.
Bonus: Change the secret, then check all three responses again. Or give the hints a personality: what would the castle's dragon say?
One guess is a rather short game. In Loops, we'll keep the decisions and give the player another chance without writing the question again.
What's Next?
Our programs can now make decisions based on the information we give them. With if, elif, and else, we've gone from programs that always follow the same path to ones with different possible outcomes.
And we've used that ability to build our first adventure game!
But there's still one problem.
Every time our adventure ends, the program stops. If we want to play again, we have to start it all over.
What if we wanted our program to keep running? What if we wanted to give the player another chance without having to repeat our code?
That's what we'll explore next.