What Are Conditions?

STEP 1 β€” What Are Conditions?

In Unit 2, your game learned how to store and update information: health, coins, XP, and more. In Unit 3, your game learns how to think about that information and make decisions.

This is where conditions come in.

1. What is a condition?

A condition is a question your program asks about the data it has.

  • Is the player’s health below 20?
  • Does the player have enough coins?
  • Has the player reached level 5?

Each condition can only be answered in two ways:

  • True
  • False

If the condition is True, the game does something.

If it is False, the game does something elseβ€”or nothing at all.


2. The if Statement

if condition:
    # code that runs when the condition is True

This is how your game starts making decisions.


3. Example: Low Health Warning

health = 15

if health < 20:
    print("Warning: Health is low!")
Click Run to execute.

  • The condition is health < 20.
  • If health is less than 20, the message is printed.
  • If health is 50, nothing happens.

This is the beginning of game logic: your game reacts to the player’s situation.


4. Example: Enter the Cave

torch = True

if torch == True:
    print("You enter the dark cave.")
Click Run to execute.

Here, the game checks if the player has a torch.
If the condition is True, the player can enter.


Practice (Your Turn!)

Mini-Challenge

Create a variable coins.

If coins is greater than or equal to 10, print:

“You can buy the sword.”
Write your code and Run.