Skip to content
techsnow

Control Flow

While Loops

A while loop repeats as long as its condition is True.

count = 3
while count > 0:
    print(count)
    count -= 1
print("Lift off!")

Output

3
2
1
Lift off!

A number guessing game

import random

secret = random.randint(1, 10)
guess = 0
while guess != secret:
    guess = int(input("Guess 1-10: "))
    if guess < secret:
        print("Too low")
    elif guess > secret:
        print("Too high")
print("Correct!")