Module 02 / Project 03 -- Interactive Prompts¶
Home: README
Learn Your Way¶
| Read | Build | Watch | Test | Review | Visualize | Try |
|---|---|---|---|---|---|---|
| — | This project | — | — | Flashcards | — | — |
Focus¶
click.prompt()to ask the user for inputclick.confirm()to ask yes/no questionsclick.echo()withclick.style()for colored terminal output- Building an interactive experience inside a CLI
Why this project exists¶
Not every CLI is a one-shot command. Sometimes you need to ask the user questions, confirm dangerous actions, or highlight results with color. Click provides prompt, confirm, and styling utilities that handle edge cases (encoding, piped input, Windows terminals) so you do not have to. This project builds an interactive quiz to put all three to work.
Run¶
cd projects/modules/02-cli-tools/03-interactive-prompts
# Run the quiz (interactive)
python project.py
# Run with a specific number of questions
python project.py --questions 3
# Skip the welcome confirmation
python project.py --no-confirm
Expected output¶
$ python project.py --questions 2
Welcome to the Python Quiz!
Ready to start? [Y/n]: y
Question 1 of 2
What keyword defines a function in Python? def
Correct!
Question 2 of 2
What built-in function returns the length of a list? len
Correct!
Final score: 2/2
Great job!
(Colors will appear in your terminal -- green for correct, red for incorrect.)
Alter it¶
- Add three new questions to the quiz bank.
- Add a
--shuffleflag that randomizes the question order. - After the quiz, prompt the user for their name and write the score to a
scores.txtfile.
Break it¶
- Replace
click.prompt()with Python's built-ininput(). Run the script and pipe input from a file (echo "def" | python project.py). Compare the behavior. - Remove the
click.style()calls and use plainclick.echo(). Note what you lose. - Change
click.confirm()toclick.prompt()with no default. What happens when the user presses Enter without typing anything?
Fix it¶
- Restore
click.prompt()and verify piped input works again. - Restore the
click.style()calls and confirm colors appear. - Restore
click.confirm()and verify that pressing Enter defaults to "yes".
Explain it¶
- What does
click.prompt()handle thatinput()does not? - How does
click.style()apply color -- what terminal standard does it use? - Why does
click.confirm(abort=True)exist and when would you use it? - What happens to colors when you redirect output to a file (
python project.py > output.txt)?
Mastery check¶
You can move on when you can:
- use click.prompt() with type conversion (e.g., type=int),
- style output with foreground color, bold, and underline,
- build an interactive flow that asks questions and acts on answers,
- explain why click.echo() is preferred over print() in Click apps.
Related Concepts¶
Next¶
Continue to 04 - File Processor CLI.