Beginner — Day 3

Lists & While Loops

Day Schedule
Online
TimeActivity
10:00Intro to Lists
Index, append, len(); demo IndexError
10:25Intro to While Loops
Slides first: condition, infinite-loop danger, Ctrl+C
10:35While Loops
Code along: countdown, then word-membership check with "in"
11:00Break
11:10Breakout Groups
Project: Guess the Word (while loop + all() check)
12:20Reminders
In-Person
TimeActivity
8:00Warm-Up
Review Day 2: if-else quick quiz
8:20Intro to Lists
Index, append, len(); demo IndexError
8:50Intro to While Loops
Slides first: condition, infinite-loop danger, Ctrl+C
9:10While Loops
Code along: countdown, then word-membership check with "in"
9:40Break
9:50Breakout Groups
Project: Guess the Word (while loop + all() check)
11:00Break
11:10Extra Activity
Add Lives: decrement a counter and break when lives reach 0
11:50Open Coding
Catch up or extend the word game
12:20Reminders
Learning Objectives
  1. Create lists of every data type
  2. Retrieve a value from a list using an index
  3. Use append() to add items and len() to count them
  4. Create a while loop with an integer condition
  5. Use "in" to check membership in a list or string
# INSTRUCTOR — Day 3: Lists & While Loops (Beginner)
# -----------------------------------------------
# TEACH: A list stores multiple values in one variable.
# Indexes start at 0. Accessing an index that doesn't exist → IndexError (demo this).

colors = ["red", "blue", "green"]
print(colors[0])            # "red"  — TEACH: index 0 is the FIRST item
print(colors[2])            # "green"
colors.append("yellow")     # adds to the end
print(colors)               # shows all four items
print(len(colors))          # TEACH: len() gives the number of items

# TEACH: while loop — runs as long as condition is True.
# Show the danger: if count never changes, the loop runs forever (infinite loop demo).
# Use Ctrl+C to stop it — good survival skill for students.

count = 5

while count > 0:
    print("Countdown: " + str(count))
    count = count - 1   # TEACH: this line is what makes the loop eventually stop

print("Go!")

# TEACH: what happens if we change count = count - 1 to count = count - 2?
# What if we forget this line entirely? Show both.

# TEACH: lists + while loops together
fruits = ["apple", "banana", "cherry"]
i = 0
while i < len(fruits):
    print(fruits[i])
    i += 1
# TEACH: this is how we step through a list manually — foreshadows for loops tomorrow
# INSTRUCTOR — Day 3: Project: Guess the Word Game (Beginner)
# -----------------------------------------------
# TEACH: This is the first project that feels like a real game.
# Key concepts: a list that grows (guessed), a while loop with two exit conditions,
# and the all() built-in to check if every letter has been found.
# Walk through the logic carefully before students build independently.

secret_word = "python"  # TEACH: change this to any word — students pick their own
guessed = []            # TEACH: starts empty, fills as correct letters are guessed
found = False

while not found:        # TEACH: "not found" means "while found is False"
    guess = input("Guess a letter: ")

    if guess in secret_word:        # TEACH: 'in' checks membership — works on strings and lists
        guessed.append(guess)
        print("Nice! Letters found so far: " + str(guessed))
    else:
        print("Not in the word. Keep trying!")

    # TEACH: Introduce all() before students see this line — it's the trickiest concept today.
    # "all() asks: does EVERY item in this group pass a test? If even one fails, it returns False."
    # Demo on the console before showing this code:
    #   all([True, True, True])    → True
    #   all([True, False, True])   → False
    # Then explain the phrase: "for letter in secret_word" goes through each letter one by one.
    # "letter in guessed" asks: have we already guessed this letter?
    # So the whole thing reads: "Has EVERY letter in the secret word been guessed?"
    if all(letter in guessed for letter in secret_word):
        print("You got it! The word was: " + secret_word)
        found = True

# TEACH: ask — what happens if the secret word has a repeated letter like "banana"?
# Does the player need to guess 'a' three times? (No — once is enough. Why?)

# TEACH: challenge — print how many wrong guesses were made.
# Students need a counter variable that increments in the else branch.
# INSTRUCTOR — Day 3: Extra Activity: Add Lives (Beginner)
# -----------------------------------------------
# TEACH: Students add a lives system to the word game.
# Key new concept: a counter that decrements on wrong guesses,
# and using 'break' to exit the loop when lives run out.
# lives > 0 as the while condition means the game ends naturally.

secret_word = "python"
guessed = []
lives = 5       # TEACH: students decide the starting value

while lives > 0:
    guess = input("Guess a letter (" + str(lives) + " lives left): ")

    if guess in secret_word:
        guessed.append(guess)
        print("Nice! Letters found: " + str(guessed))
    else:
        lives = lives - 1       # TEACH: this is what makes lives run out
        print("Wrong! " + str(lives) + " lives left.")

    if all(letter in guessed for letter in secret_word):
        print("You got it! The word was: " + secret_word)
        break   # TEACH: break exits the while loop immediately

# TEACH: this prints AFTER the loop ends
# Is it always "game over"? No — they could have won via break. How would we fix that?
# Discuss: a 'won' flag variable. Good foreshadowing for tomorrow.

# Challenge extension:
if lives == 0:
    print("Game over! The word was: " + secret_word)