State Machines

A state machine is like a flowchart that shows how something changes from one situation (called a state) to another.

Example: think about a traffic light.

  • It has three states: green, yellow, and red.

  • It only changes in certain ways:

    • Green → Yellow

    • Yellow → Red

    • Red → Green

The machine “remembers” which state it’s in and changes only when something tells it to — like a timer or a button.

You can think of it as a set of rules that say:

  • “If I’m in this state, and this happens, move to that state.”

Computers use state machines to keep track of what they’re doing — for example, whether a game character is walking, jumping, or falling.

Example Code

Below is a simple example of a state machine implemented in Python:

 1light_on = True
 2
 3while True:
 4    # Display current state
 5    if light_on:
 6        print("The light is ON")
 7    else:
 8        print("The light is OFF")
 9    
10    # Get user input
11    command = ""
12    while command != "on" and command != "off":
13        command = input("Turn light on or off? ").strip().lower()
14    
15    # Update state based on input
16    if command == "on":
17        light_on = True
18    else:
19        light_on = False
20