Chapter 16: While Loopsยถ
๐ Open Notebookยถ
๐บ Video Tutorialยถ
While loops in Python are easy! โพ๏ธ (8:04)
๐ What Youโll Learnยถ
Execute code repeatedly while a condition is true - essential for creating interactive programs!
๐ฏ Learning Objectivesยถ
Understand while loop syntax
Create loops with conditions
Use while loops for user input validation
Avoid infinite loops
Use break and continue statements
๐ Concept Explanationยถ
What is a While Loop?ยถ
A while loop repeatedly executes code as long as a condition remains True.
Syntaxยถ
while condition:
# code to execute
# update condition eventually!
How It Worksยถ
Check if condition is True
If True, execute the code block
Go back to step 1
If False, exit the loop
๐ก Examplesยถ
Example 1: Age Verificationยถ
age = int(input("Enter your age: "))
while age < 18:
print("You are a minor")
age = int(input("Enter your age: "))
print("You are an adult!")
Example 2: Countdownยถ
count = 5
while count > 0:
print(count)
count -= 1
print("Blastoff!")
Example 3: Sum Numbers Until Zeroยถ
total = 0
number = int(input("Enter a number (0 to stop): "))
while number != 0:
total += number
number = int(input("Enter a number (0 to stop): "))
print(f"Total: {total}")
Example 4: Password Validatorยถ
password = input("Enter password: ")
while len(password) < 8:
print("Password must be at least 8 characters!")
password = input("Enter password: ")
print("Password accepted!")
โ๏ธ Practice Exercisesยถ
Create a number guessing game
Keep asking for positive numbers until user enters negative
Validate email format (must contain @)
Create a simple menu system
Build a countdown timer
Make a multiplication drill program
๐ Loop Control Statementsยถ
break - Exit Loop Earlyยถ
count = 0
while True: # Infinite loop
count += 1
if count > 5:
break # Exit loop
print(count)
# Prints: 1, 2, 3, 4, 5
continue - Skip Current Iterationยถ
count = 0
while count < 10:
count += 1
if count % 2 == 0:
continue # Skip even numbers
print(count)
# Prints: 1, 3, 5, 7, 9
๐ Common Mistakesยถ
Infinite Loopsยถ
# Wrong: Condition never becomes False
count = 1
while count > 0:
print(count) # Runs forever!
# Correct: Update condition
count = 5
while count > 0:
print(count)
count -= 1 # Eventually becomes 0
๐ฎ Real-World Examplesยถ
ATM Machineยถ
balance = 1000
while True:
print("\n1. Check Balance")
print("2. Withdraw")
print("3. Exit")
choice = input("Choose: ")
if choice == "1":
print(f"Balance: ${balance}")
elif choice == "2":
amount = float(input("Amount: "))
if amount <= balance:
balance -= amount
print(f"Withdrawn: ${amount}")
else:
print("Insufficient funds")
elif choice == "3":
break
else:
print("Invalid choice")
๐ Challenge Projectsยถ
Login System: 3 attempts before lockout
Shopping Cart: Add items until user is done
Grade Calculator: Keep accepting scores until user enters โdoneโ
Dice Game: Roll until you get 6
Calculator Loop: Perform calculations until user quits
๐ Key Takeaways from Videoยถ
Strings are text data enclosed in quotes
Use loops to repeat actions
Use if-elif-else for conditional logic
๐ก These points cover the main concepts from the video tutorial to help reinforce your learning.
๐ Next Chapterยถ
Continue to Chapter 17: Interest Calculator to build a compound interest calculator!