Anatomy of a Python If Statement

From saving throws to advantage rolls, learn how Python’s if, elif, and else conditionals let your code make decisions.

Anatomy of a Python If Statement
Photo by Artturi Jalli / Unsplash

So far, we’ve built functions to roll dice and used loops to spam fireballs. But what makes a tabletop RPG exciting isn’t just rolling numbers — it’s the decisions and consequences.

  • Did your rogue actually dodge the trap?
  • Does the paladin succeed on a saving throw?
  • Does the wizard get advantage or eat dirt?

In code, those branching paths are handled with conditionals — the if, elif, and else statements in Python. They let your program say: “If this happens, do one thing. Otherwise, do something else.”


What Is a Conditional in Python?

A conditional is just a decision point. It’s Python asking: “Is this true?”

If yes → run the block of code.
If no → skip it (or fall back to an else).


Basic Example: Saving Throw

Let’s say you need to roll a Constitution saving throw against poison. The DC (difficulty class) is 15.

import random

def poison_save():
    roll = random.randint(1, 20)
    if roll >= 15:
        return "Success! You resist the poison."
    else:
        return "Failure! You take damage."

Here’s what happens:

  • Roll a d20.
  • If the roll is 15 or higher, success.
  • Otherwise, fail.

That’s a conditional in action — your code splits into different outcomes.



Chaining Conditions with elif

What if we want to handle criticals? A natural 20 should be an instant success, and a natural 1 should be an instant failure.

def poison_save():
    roll = random.randint(1, 20)
    if roll == 20:
        return "Critical Success! You shrug it off."
    elif roll == 1:
        return "Critical Failure! The poison overwhelms you."
    elif roll >= 15:
        return "Success! You resist the poison."
    else:
        return "Failure! You take damage."

The elif lets us check multiple branches in order. Python runs the first one that’s true and skips the rest.


Nested Conditionals: Advantage/Disadvantage

D&D players know the pain and joy of rolling with advantage or disadvantage. Let’s code that up.

def roll_with_advantage(dc=15):
    roll1 = random.randint(1, 20)
    roll2 = random.randint(1, 20)
    best = max(roll1, roll2)

    if best >= dc:
        return f"Rolls: {roll1}, {roll2} → Success!"
    else:
        return f"Rolls: {roll1}, {roll2} → Failure."

The conditional decides success based on the higher of the two rolls. Swap max for min and you’ve got disadvantage.


Conditionals Beyond Games

Yes, dice are fun. But conditionals drive everything in production code too:

  • Auth checks: if user.is_authenticated: ...
  • API responses: if status_code == 200: ...
  • Error handling: if not data: return "Bad request"

They’re the “decision engine” of your program.


Best Practices for Conditionals

From hobby scripts to production backends, a few lessons stick:

  • Keep them readable — don’t chain ten elifs if you can use a dictionary or mapping (or the new match statements).
  • Watch for nesting hell. Refactor deep nesting into helper functions.
  • Name your booleans clearly: if is_admin: beats if x: every time.

Conclusion

Conditionals are where your code makes choices. In Python, that’s as simple as if, elif, and else. Whether you’re rolling a d20 to resist poison or checking an API’s status code, the principle is the same: evaluate a condition, branch into the right outcome.

So the next time you hear someone say “if statements are basic,” remember this: they’re the invisible dice rolls happening under the hood of almost every program you’ve ever used.