Making decisions: if, elif, else
Branch your program with if/elif/else, master indentation as syntax, and combine conditions with and, or, not.
What You'll Learn
- Write if/elif/else conditions to make decisions in your program
- Use indentation (4 spaces) to mark which lines belong to a block
- Combine conditions with the logical operators and, or, not
- Tell apart = (assignment) from == (comparison)
Last lesson you built the hero's character sheet, name, HP, attack, gold, and changed those stats with math. But the game still runs every line top to bottom and never chooses. Today it learns to decide: enough gold to buy the sword, or not?
Conditions (if / elif / else)
So far your code has run straight down the page, every line, every time. A game can't work like that. It has to choose: if HP hits 0 the character dies, if you have enough gold you can buy the weapon, if you're level 10 you unlock a skill.
That choice is a condition. You write it with if. The if line ends with a colon :, which means "here comes the block that belongs to me," and the lines under it are pushed right (indented) to show they belong to the if. The structure looks like this:
If we also want to handle the case where the player is alive, we add else:
When you have multiple options, you use elif (short for "else if"):
Python checks conditions from top to bottom. The first condition that is true gets executed, and the rest are ignored. If none are true, the else block runs.
Indentation
Look back at those if blocks. The lines that belong to the if are pushed 4 spaces to the right. That push is called indentation, and in Python it is the rule, not a style choice. The spaces are how Python knows which lines run when the condition is true.
Both print lines sit 4 spaces in, so both belong to the if. Line them up and the block holds together. The next example breaks on purpose: drop the spaces and Python loses track of where the block starts, so it stops with an error. Run it so you recognize the message later, an error here means nothing is wrong with you, it is how Python tells you the indentation slipped:
Most languages wrap blocks in curly braces {}. Python uses the indentation itself, which keeps the code clean as long as you stay consistent: pick 4 spaces or a tab and never mix the two in one file. This trips up almost everyone at first. When you hit an IndentationError, the fix is nearly always a line that's spaced differently from its neighbors.
Logical operators (and, or, not)
One condition is often not enough. To buy the rare sword the hero might need a high enough level and enough gold, both at once. To survive a hit they might need either a shield or armor, just one will do. Logical operators let you join conditions like that. There are three: and, or, and not.
and - both conditions must be true
Both conditions are true (8 >= 5 and 200 >= 150), so the success message is displayed.
or - at least one condition must be true
Even though you do not have a shield, you have armor, and that is enough when using or.
not - reverses a condition
not False becomes True, so the first block runs.
You can combine multiple logical operators in a single condition:
The difference between = and ==
This one catches almost every beginner, so pin it down now and save yourself an afternoon of confusing bugs.
A single = means assignment, you give a value to a variable:
Two equals == means comparison, you check if two values are equal:
If you accidentally write = instead of == in an if, Python will give you an error. Remember: one equal sets, two equals asks.
Conditions make decisions. Python reads if/elif/else from top to bottom and runs the first true block, indentation marks which lines belong to it, and and/or/not let you join conditions. Remember: one equal (=) sets a value, two equals (==) compares values.
Exercise : Hero class selection
Exercise : Kingdom Shop
Exercise : Attack with critical hit
Exercise : Rank system
Complete game code: Python Kingdom
Here's your game so far: the title screen plus character creation with a class and a shop, now that conditions let it actually decide things.
One thing still bugs me about it. Every stat is hardcoded. You decide the hero's name and class, the player never does. Next lesson you hand the keyboard over: input() lets the player type their own name and pick their own class while the game runs. Your if/elif/else branches stop reacting to numbers you wrote and start reacting to choices the player makes.
Test Your Knowledge
Check how well you understood the lesson with these 4 questions.

