3.2.3 Sentinel Values and State Flags

Unit: Unit 3: Loops • Interactive Practice

Quick Answer & Summary:

Interactive Python programming tutorial and practice in your browser.

Interactive Challenge (Run Live in Browser):

# Simulate processing user commands using a sentinel flag
is_running = True
command_queue = ["status", "sync", "exit", "reboot"]
index = 0

while is_running:
    current_cmd = command_queue[index]
    print("Processing command:", current_cmd)
    
    if current_cmd == "exit":
        print("Sentinel value 'exit' detected. Powering down...")
        is_running = False  # Toggle the state flag to cleanly stop the loop
    
    index += 1

print("System halted successfully.")
Open in Live Interactive Python Sandbox →