Phase 3 🎮 — Control Flow
Topics: if-elif-else, for loops, while loops, break/continue, comparisons
Make your programs smart. Control flow lets your code make decisions, repeat tasks, and respond to different conditions.
🔄 Exercise Flow
📚 Prerequisites
Before starting these exercises, make sure you've read:
- If-Else Statements — Conditional logic in Python
- Loops —
forandwhileloops - Break, Continue, Pass — Loop control statements
- Comparisons — Comparison and logical operators
🔰 Starter: Number Guessing Game (Simple)
Time: 12 minutes
Build a number guessing game where the computer picks a number and the user guesses it with hints.
Learning Objectives
- Use
whileloops for repeated execution - Use
if-elif-elsefor conditional logic - Compare values with
<,>,== - Use
breakto exit a loop
Starter Code
# Starter: Number Guessing Game
import random
secret = random.randint(1, 10)
guess = None
print("I'm thinking of a number between 1 and 10.")
# TODO: Use a while loop to keep asking until the user guesses correctly
# TODO: If guess < secret, print "Too low!"
# TODO: If guess > secret, print "Too high!"
# TODO: If guess == secret, print "Correct!" and break
# Your code here 👇
Expected Output
I'm thinking of a number between 1 and 10.
Your guess: 5
Too low!
Your guess: 8
Too high!
Your guess: 7
Correct! 🎉
⭐ Medium: FizzBuzz With a Twist
Time: 20 minutes
Implement FizzBuzz with extended rules, using both for and while loops, and allow the user to set custom rules.
Learning Objectives
- Use
forloops withrange() - Combine
if-elif-elsewith modulo operator - Use
continueto skip iterations - Accept and validate user configuration
Starter Code
# Medium: FizzBuzz With a Twist
# TODO: Ask the user for two numbers (fizz_num, buzz_num)
# TODO: Ask the user for the max range (e.g., 50)
# TODO: Loop from 1 to max and print:
# - "Fizz" if divisible by fizz_num
# - "Buzz" if divisible by buzz_num
# - "FizzBuzz" if divisible by both
# - The number otherwise
# TODO: Skip numbers that contain the digit 7 (use continue)
# TODO: Stop early if the number is a multiple of both fizz_num and buzz_num squared
# Your code here 👇
Expected Output
Enter Fizz number: 3
Enter Buzz number: 5
Enter max range: 30
1
2
Fizz
...
14
FizzBuzz (stopped early — 15 is 3*5)
🏆 Hard: Interactive Menu System
Time: 35 minutes
Build a fully interactive restaurant menu system that uses nested conditionals, loops, and control flow to manage orders, apply discounts, and generate bills.
Learning Objectives
- Design nested
if-elif-elsestructures for a multi-level menu - Use
whileloops for persistent interaction until the user quits - Combine
breakandcontinuefor navigation flow - Handle invalid input gracefully
- Build a complete interactive CLI application
Starter Code
# Hard: Interactive Menu System
# Menu items with prices
menu = {
"Burger": 5.99,
"Pizza": 8.99,
"Pasta": 7.49,
"Salad": 4.99,
"Fries": 2.99,
"Soda": 1.99,
"Water": 0.99
}
# TODO: Build an interactive system that:
# 1. Shows the menu and asks user to pick items (by name or number)
# 2. Lets user add items to an order (with quantities)
# 3. Shows the current order and total at any time
# 4. Applies discount rules:
# - 10% off if order > $20
# - Free drink if order > $30
# 5. Lets user remove items or change quantities
# 6. Generates a formatted bill when user types "done"
# 7. Handles invalid inputs without crashing
print("🍽️ Welcome to Python Diner!")
print("Type 'menu' to see items, 'order' to see your cart, 'done' to finish.\n")
order = {}
# Your code here 👇
Expected Output
🍽️ Welcome to Python Diner!
Type 'menu' to see items, 'order' to see your cart, 'done' to finish.
> menu
1. Burger - $5.99
2. Pizza - $8.99
3. Pasta - $7.49
4. Salad - $4.99
5. Fries - $2.99
6. Soda - $1.99
7. Water - $0.99
> add Burger 2
2x Burger added ✅
> add Pizza 1
1x Pizza added ✅
> order
📋 Current Order:
Burger x2 = $11.98
Pizza x1 = $8.99
Total: $20.97
> done
═══════════════════════
PYTHON DINER BILL
═══════════════════════
Burger x2 $11.98
Pizza x1 $8.99
───────────────────────
Subtotal: $20.97
Discount (10%): -$2.10
───────────────────────
Total: $18.87
───────────────────────
Thanks for dining! 🎉
💡 Tips for Success
- Indentation matters — Python uses indentation to determine which code is inside a block. Use 4 spaces consistently.
- Test edge cases — What happens when the user enters a negative number? A string? Nothing?
- Use
while Truefor infinite loops that you break out of manually — it's clean and readable. - Combine conditions with
and,or,notto write compactifstatements.