Python Basics — Day 7 While Loops + Mini Projects
Aspiring Full-Stack Developer | Python · Django · React · SQL | documenting my learning journey
Building skills in Python and full-stack development, with a focus on web apps and system design
Junior developer in training — Python, Django, React — preparing for a career in full-stack engineering
From Python basics to full-stack projects, sharing my progress as I grow into a developer
Future full-stack engineer | Learning in public: Python, APIs, Databases, and Web Development
01. Basic Structure of a while Loop
Repeats as long as the condition is True
Stops when the condition becomes False
count = 1
while count <= 5:
print("Hello", count)
count += 1
👉 Output:
Hello 1
Hello 2
Hello 3
Hello 4
Hello 5
02. Infinite Loops with while
If the condition is always True, the loop runs forever.
Usually, break is used to exit.
while True:
cmd = input("Enter q to quit: ")
if cmd == "q":
print("Program terminated!")
break
03. Using continue
Skip the current iteration and move to the next one.
num = 0
while num < 10:
num += 1
if num % 2 == 0:
continue
print(num) # Prints only odd numbers
04. While vs For
for loop → when the number of repetitions is fixed
while loop → when repetitions depend on a condition and may vary
05. Practice Examples
Example 1: Print Numbers 1 to 10
n = 1
while n <= 10:
print(n)
n += 1
Example 2: Password Checker
password = "1234"
while True:
guess = input("Enter password: ")
if guess == password:
print("Login successful!")
break
else:
print("Incorrect. Try again.")
06. Mini Projects ✨
(1) Multiplication Table
dan = int(input("Enter a number for the times table: "))
i = 1
while i <= 9:
print(f"{dan} x {i} = {dan * i}")
i += 1
(2) Sum Calculator
Keeps adding numbers entered by the user until 0 is typed.
total = 0
while True:
num = int(input("Enter a number (0 to quit): "))
if num == 0:
break
total += num
print("Total sum:", total)
(3) Number Guessing Game
import random
secret = random.randint(1, 20) # Random number between 1 and 20
tries = 0
while True:
guess = int(input("Guess the number (1–20): "))
tries += 1
if guess == secret:
print(f"Correct! You guessed it in {tries} tries.")
break
elif guess < secret:
print("Try a higher number.")
else:
print("Try a lower number.")