By the end of this lesson
Separate raw text, conversion and a simple range rule.
Before you start
- Python 3.12 or a compatible newer Python 3 interpreter; no external packages.
- Complete lesson 4 in this route, or be comfortable with its examples.
Three words to know
- Input
- Data supplied to a program.
- Validation
- Checking whether input follows your stated rules.
- ValueError
- An exception that can signal a failed conversion.
1. Input arrives as text
input reads one line from the terminal and returns a string without its ending newline. Type a value and press Enter. This example catches one expected conversion error; the full error-handling lesson explains try and except in more depth. Do not use eval to turn user text into a value.
In the output example, 4 is what you type at the prompt; the terminal normally echoes it. The program itself prints the prompt and the result.
raw = input("Pages: ")
try:
pages = int(raw)
except ValueError:
print("Enter a whole number.")
else:
if 1 <= pages <= 50:
print(f"Checking {pages} pages")
else:
print("Choose 1 to 50 pages.")
What you should see · example output
Pages: 4 Checking 4 pages
2. Conversion is not the whole rule
int accepts more than ASCII digits, including surrounding whitespace and a leading sign. That is acceptable for this practice prompt because the rule is simply a convertible whole number in a range. A strict file format would need a separate grammar. A valid integer such as 0 still fails our range rule.
3. Test more than the happy path
Run the program with 4, 0 and apple. Expect a success message, a range message and a conversion message respectively. Replacing the result with a silent default would hide a mistake. This interactive teaching script prints friendly messages and is not yet a CLI with failure exit codes.
YOUR TURN
A small check before you continue
Try 51, -1 and 12. Which inputs convert successfully and which meet the allowed range?
Show a suggested answer
All three convert to integers. Only 12 meets the 1-to-50 rule. The other two receive the range message, not the conversion message.
Sources and scope
Documentation and examples reviewed 2026-10-18. Provider screens, package versions and output wording can differ. Examples show expected outcomes; they are not a claim that OpenViST connected to your server or verified your setup.
