How it works
Run these examples in a local terminal or an editor with interactive console input. Codectionary’s browser runner does not provide a Python stdin prompt. For browser practice, replace an input call with a sample string such as "21"; this simulates a response without asking the visitor to type.
input() does not evaluate Python code and does not convert digits to numbers. int("21") produces an integer; int("twenty") raises ValueError. Catch that conversion error, then check the business rule separately: a positive integer must be greater than zero.
When the input stream ends, input() raises EOFError rather than returning an empty string. Pressing Enter on an empty line returns "". A terminal user can interrupt input, normally producing KeyboardInterrupt. The validation example handles end-of-input so automated or redirected input can finish cleanly.
Common mistakes and boundaries
Do not use eval() to turn untrusted input into a value. Use a specific parser such as int() instead.
isdigit() is not an integer parser: it rejects signed text and accepts some Unicode digits that int() cannot parse. Successful conversion alone also does not establish that a value is positive.
Ordinary input() echoes typed characters in a terminal. Do not use these examples to collect real passwords; password input requires an appropriate environment and a tool such as getpass.
Try it yourself
Run the validation loop with blank input, abc, 0, -2 and finally 7. It should reject the first four and accept 7. Also test end-of-input. Keep conversion errors separate from the positive-number check.
Syntax
variable = input(prompt)Examples
Basic Input
Prompting the user and reading their response.
name = input("What is your name? ")
print(f"Hello, {name}!")- For the sample response John, input returns "John" and the f-string includes it in the greeting. The transcript shown excludes terminal echo of what you type.
- Expected output with the sample input below: What is your name? Hello, John!
- Run in a local terminal or an editor with console input. Sample input, one response per line: John
Converting Input to a Number
Since input() always returns a string, convert it before doing arithmetic.
age_text = input("Enter your age: ")
age = int(age_text)
print(f"Next year you will be {age + 1}")- This deliberately simple version assumes valid integer text; the next example handles conversion failures. With 21 it prints the following age.
- Expected output with the sample input below: Enter your age: Next year you will be 22
- Run in a local terminal or an editor with console input. Sample input, one response per line: 21
Input Validation Loop
Repeating a prompt until the user provides valid input.
while True:
try:
value = input("Enter a positive integer: ")
except EOFError:
print("No more input.")
break
try:
number = int(value)
except ValueError:
print("Please enter an integer.")
continue
if number <= 0:
print("The number must be greater than zero.")
continue
print(f"You entered {number}")
break- try/except handles malformed text, a separate comparison rejects zero and negatives, and break ends the loop after a valid value. EOF exits with a message instead of a traceback.
- Expected output with the sample input below: Enter a positive integer: Please enter an integer. Enter a positive integer: The number must be greater than zero. Enter a positive integer: The number must be greater than zero. Enter a positive integer: You entered 7
- Run in a local terminal or an editor with console input. Sample input, one response per line: abc 0 -2 7
Best practices
- Always convert input() results with int(), float(), etc. before doing arithmetic - they arrive as strings
- Wrap numeric conversions in try/except to handle invalid input gracefully rather than letting the program crash
- Use a clear, specific prompt string so the user knows exactly what format is expected
- Validate input in a loop rather than accepting the first (possibly invalid) response silently
Runtime & versions
Python interpreter. Feature availability depends on the language and runtime version used by your project; consult the official reference below.
Python language reference