This module teaches defensive input handling for interactive programs.
- Difficulty: Intermediate.
- Estimated Time: 30-45 minutes.
- Prerequisites:
01-foundations/control-flow,01-foundations/types-and-io. - Cross-Language Lens: Compare loop-driven validation in all four languages and notice where parsing APIs are strict versus forgiving.
python example/main.py- Validating integer and floating-point input with retry loops.
- Rejecting values outside accepted ranges.
- Reusing input-validation helpers to avoid duplicated logic.
- Keeping interactive programs stable when users type invalid data.
- Calling
int()orfloat()once and crashing on invalid input. - Accepting out-of-range values after successful parsing.
- Repeating similar validation logic instead of extracting helpers.
- exercises/01.py: read an integer in range 1 to 100 and print its square.
- exercises/02.py: read a valid score count and valid scores, then print average.
- exercises/01.py
- Input: repeated attempts until a valid integer in range 1..100 is entered.
- Output: square of the accepted value.
- Edge cases: non-integer text; values below 1 or above 100.
- exercises/02.py
- Input: score count in range 1..50, followed by scores in range 0..100.
- Output: average score.
- Edge cases: invalid value in the middle of score entry; boundary values 0 and 100.
- I can recover from type parsing errors without crashing.
- I can validate numeric ranges with retry loops.
- I can reuse helper functions for multiple validated inputs.
- I completed exercises/01.py.
- I completed exercises/02.py.