← Module 5 Module 7 →

⌨️ Module 6

User Input and While Loops

So far programs mostly talked <em>at</em> you. Now they listen — and keep asking until you get it right.

A teen types at a keyboard while a crystal ball asks a question and a tiny frog offers a pebble.
Patch’s input is always "meow". A while loop is doing its best.

🪄 The concept

input() pauses and waits for the player to type. It always returns a string, so convert with int() before doing math or number comparisons.

A while loop repeats as long as a condition is true — perfect for guessing games and menus. break jumps out early when the player quits.

Real humans type messily. .strip() and .lower() clean spaces and CAPS so " LUMOS " still matches "lumos".

💬 Discussion

Talk these through with a friend, a class, or just your notes app:

  1. What apps ask you questions in a loop until you answer “correctly” or quit?
  2. Why is cleaning input (strip/lower) kinder to players than rejecting typos harshly?
  3. When could an infinite loop (while True) be useful — and how do you make sure it can end?

💻 Code walkthrough

Copy into your editor, or download the .py file and run it. Tinker — change names, numbers, and spells.

Reading and converting user input user_input.py

# input() pauses the program and waits for the player to type
name = input("What is your wizard name? ")
print("Nice to meet you,", name)

# Always convert before comparing numbers or doing math
age_text = input("How old are you? ")
age = int(age_text)
if age >= 11:
    print("You may receive your Hogwarts letter!")
else:
    print("A few more years of Muggle school...")

While loop — number guessing game guessing_game.py

# The loop keeps running until the player guesses the secret number
secret = 7
guessed = False

while not guessed:
    guess_text = input("Guess the number of galleons (1-10): ")
    guess = int(guess_text)

    if guess == secret:
        print("Correct! The vault opens.")
        guessed = True
    elif guess < secret:
        print("Too low — try again.")
    else:
        print("Too high — try again.")

Cleaning input with .strip() and .lower() cleaning_input_with_strip_and_lower.py

# Players type extra spaces or CAPS — normalize before you compare
answer = input("Cast Lumos or Nox? ")
answer = answer.strip().lower()   # "  LUMOS  " becomes "lumos"

if answer == "lumos":
    print("Light!")
elif answer == "nox":
    print("Darkness.")
else:
    print("The wand sputters — unknown spell.")

Menu loop with break to quit menu_loop.py

# while True runs forever until break stops the loop
while True:
    print("\n--- Common room ---")
    choice = input("(t)udy, (s)leep, (q)uit: ").strip().lower()

    if choice == "q":
        print("Goodnight!")
        break          # jump out of the loop
    elif choice == "t":
        print("You open your textbook...")
    elif choice == "s":
        print("You rest by the fire.")
    else:
        print("Pick t, s, or q.")

🧪 Try this

  1. Change the secret number in guessing_game.py, then play until you win. Add a counter that prints how many guesses you needed.
  2. Extend the common-room menu with a third option (snack? owl post?). Keep q as quit.
  3. Break input on purpose: type letters when the program expects a number. What error appears? How might you guard against it later?

📁 Local files

All runnable Python for this module:

module-06-input-while/ · all modules

🎬 Video lesson

← Module 5 Module 7 →