Aral Code

Decisions & Loops Lesson 12 of 23

Python while Loops

Repeat code while a condition is true — counters, savings goals, input validation and menus — and control loops with break, continue and else.

A loop repeats code. A while loop keeps repeating as long as a condition is true. It's perfect when you don't know in advance how many times you'll need to repeat.

A simple counter

count = 1
while count <= 5:
    print("Count:", count)
    count += 1
print("Done!")

Output

Count: 1
Count: 2
Count: 3
Count: 4
Count: 5
Done!

Each time through, Python checks count <= 5. When count becomes 6, the condition is false and the loop ends.

Reaching a goal

goal = 1000
savings = 0
weeks = 0

while savings < goal:
    savings += 150
    weeks += 1

print(f"It takes {weeks} weeks to save ₱{goal}.")
print(f"You'll have ₱{savings}.")

Output

It takes 7 weeks to save ₱1000.
You'll have ₱1050.

Asking until the input is valid

grade = int(input("Enter a grade (0-100): "))
while grade < 0 or grade > 100:
    print("Invalid! Try again.")
    grade = int(input("Enter a grade (0-100): "))
print("Saved:", grade)

Output

Enter a grade (0-100): 150
Invalid! Try again.
Enter a grade (0-100): -5
Invalid! Try again.
Enter a grade (0-100): 88
Saved: 88

while True with break

A common pattern is an "endless" loop that you exit with break:

total = 0
while True:
    entry = input("Price (or 'done'): ").strip()
    if entry == "done":
        break
    total += float(entry)
print(f"Total: ₱{total:.2f}")

Output

Price (or 'done'): 25
Price (or 'done'): 8.50
Price (or 'done'): 15
Price (or 'done'): done
Total: ₱48.50

continue: skip to the next round

n = 0
while n < 10:
    n += 1
    if n % 3 == 0:
        continue      # skip multiples of 3
    print(n, end=" ")

Output

1 2 4 5 7 8 10

A menu program

balance = 500

while True:
    print("\n1) Check balance  2) Deposit  3) Withdraw  4) Exit")
    choice = input("Choose: ")
    if choice == "1":
        print(f"Balance: ₱{balance}")
    elif choice == "2":
        balance += int(input("Amount: "))
    elif choice == "3":
        amount = int(input("Amount: "))
        if amount > balance:
            print("Kulang ang balance!")
        else:
            balance -= amount
    elif choice == "4":
        print("Salamat!")
        break
    else:
        print("Invalid choice.")

Output

1) Check balance  2) Deposit  3) Withdraw  4) Exit
Choose: 3
Amount: 800
Kulang ang balance!

1) Check balance  2) Deposit  3) Withdraw  4) Exit
Choose: 4
Salamat!

This "loop forever, show a menu, act on the choice" structure is used in countless console programs, including many TVL programming exercises.

while…else

A loop can have an else block that runs only if the loop ended normally, without break:

tries = 3
while tries > 0:
    pin = input("PIN: ")
    if pin == "1234":
        print("Welcome!")
        break
    tries -= 1
else:
    print("Card blocked. Too many wrong attempts.")

Output

PIN: 1111
PIN: 0000
PIN: 9999
Card blocked. Too many wrong attempts.

Quick Quiz

Check what you learned. Walang grade ito, practice lang. 🙂

1. When does a while loop stop?
2. What does break do?
3. What stops a stuck program in the terminal?
4. When does the else part of a while loop run?

By Sir Reyel · Last updated 2026-12-04