Python while Loop

1) Print 1–10 Using while

Use a while loop to print numbers 1 to 10.


2) Print Even Numbers 2–20

Use a while loop to print even numbers from 2 to 20.


3) Count Down 10–1

Print numbers 10 down to 1 using a while loop.


4) Sum of First N Numbers

Ask the user for n. Use a while loop to compute and print the sum from 1 to n.


5) Multiplication Table

Ask user for a number x. Use while loop to print table for x from 1 to 10.


6) Print Characters of a String

Ask for a word. Use a while loop to print each character one per line.


7) Count Down by 2s

Start from 20 and use while to print 20, 18, 16, … down to 2.


8) User Input Until Quit

Ask the user to enter input repeatedly until they type "quit".


9) Loop Until Valid Number

Ask for a number ≥ 0. Keep asking until they provide a valid non-negative number.


10) Factorial Calculation

Ask user for a number n. Use while to compute n! and print.

Example: if n = 4, result = 24.


11) Guess the Number (Simple)

Set a secret number inside your code. Ask the user to guess until they get it right.


12) Print a Pattern

Use while to print:


13) Reverse a Number

Ask user for an integer and use a while loop to reverse it numerically.

Example: input 123 → output 321.


14) Count Vowels in String

Ask for a string. Use a while loop to count vowels (a, e, i, o, u).


15) Count Digits

Ask for a number and use while to count how many digits it has.


16) Print Squares

Use while loop to print square of numbers 1 to 10.


17) Stop at First Even

Ask for multiple numbers (loop). Stop (using break) as soon as an even number is entered.


18) Loop with Continue

Use while to print numbers 1 to 10, but skip multiples of 3.


19) Print Multiples of 5 Until 50

Use while to print:


20) User Login Attempts

Ask user for password. Allow up to 3 attempts using a while loop. Print Access granted if correct, else Access denied after 3 tries.


📝 Example Output Pattern

Count Down 10–1:


canvil: 333f6c7d-6876-4f7e-b6ad-1bdb2233f5c1

Last updated