📖 Worked Example: Number Guessing Game

Follow along as we build a number guessing game using while loops!

Common While Loop Patterns

Counter Pattern

count = start_value
while count <= end_value:
    # do something
    count = count + 1

Sentinel Pattern

value = initial_value
while value != sentinel:
    # process value
    value = get_next_value()

Validation Pattern

while True:
    user_input = input(prompt)
    if is_valid(user_input):
        break
    print("Try again")
Next: Trace Execution → ← Back to Predict