Victor Jeman Academy
Python Fundamentals11 min

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)

if / elif / else
HP: 30/150
ifhp <= 0
elifhp < 25%HP foarte scazut! Potiune!
elifhp < 50%
elseelse

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)

Operatori logici: and, or, not
and
A and B =False
or
A or B =True
not
not A =True

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

The hero can be a Warrior, Mage, or Archer. Create a variable hero_class with one of these values. Then use if/elif/else to apply different bonuses:

  • Warrior: +20 HP, +5 attack
  • Mage: +5 HP, +10 attack
  • Archer: +7 attack, +5 defense

Start with the base values: hp=100, attack=15, defense=10. After applying the bonuses, display the new stats.

Exercise : Kingdom Shop

The hero has 50 gold and enters the shop. Create two variables: sword_price = 30 and shield_price = 25. The hero wants to buy the sword. Check if they have enough gold. If yes, subtract the price from gold, add +8 to attack, and display a success message with the remaining gold. If not, display how much gold is missing. Then check if they can also buy the shield with the remaining gold.

Exercise : Attack with critical hit

Simulate a hero's attack on a monster. Your Warrior took the class bonus and bought the Iron Sword, so their attack is now 28. The monster has monster_hp = 40 and monster_defense = 5. Calculate the damage: damage = attack - monster_defense. If the damage is greater than 20, it is a critical hit and the damage is doubled. Apply the damage to the monster's HP and display whether the monster was defeated or how much HP it has left.

Exercise : Rank system

Write a program that determines a player's rank based on their score. Create a variable player_score with a value of your choice and display the rank according to these rules:

  • Score >= 1000: rank "Legendary"
  • Score >= 500: rank "Veteran"
  • Score >= 100: rank "Adventurer"
  • Score below 100: rank "Beginner"

Test the program with several different values.

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.

Question 1 of 4

What is the difference between = and ==?