← Python Crash Course

PCC·07 · Chapter record

User Input and while Loops

Chapter 7 — programs that keep running until you tell them to stop

Done

Five sessions on interactive programs: reading input(), while loops, flags/break/continue, using while loops to shuffle items between lists, and filling a dictionary from repeated user input. Each one explains the concept, lists the key terms, then gives you exercises — try them yourself before revealing the code.

7.1

input() & Numerical Input

  • input() pauses the program and waits for typed text, then hands it back as a string — always, even if it looks like a number.
  • Give input() a clear prompt as its argument, with a trailing space so the user's typed text doesn't run into it.
  • Convert with int() (or float()) before doing math or numeric comparisons — comparing a raw string to a number raises a TypeError.
  • The modulo operator % returns the remainder of a division — number % 2 == 0 is the classic even/odd test.
input()
pauses the program and returns whatever the user typed, as a string
prompt
the message passed to input() telling the user what to enter
% (modulo)
returns the remainder of a division
Activity 1

Ask the user what kind of rental car they'd like, then print a message about it.

Activity 2

Ask how many people are in a dinner party. If it's more than 8, print that they'll have to wait; otherwise say their table is ready.

▶ Rollercoaster Checker

▶ Modulo Lab