Definition
A third-party module that simplifies input validation by providing specialized functions for common data types with built-in retry logic.
Why It Matters
User input is the primary source of program crashes and security vulnerabilities. PyInputPlus automates the “defense” of your program. Without it, you write brittle code that breaks the moment a user enters a typo; with it, you build robust systems that are “stubborn” enough to ensure data integrity without bloated manual logic.
Core Concepts
- Specialized Functions:
inputInt(),inputNum(),inputChoice(),inputEmail(),inputYesNo(),inputPassword(). - Validation Arguments:
min,max,greaterThan,lessThan: Numeric range constraints.blank=True: Allows the user to enter nothing (returns empty string).
- Stubbornness Control:
limit=n: RaisesRetryLimitExceptionafter failed attempts.
- How to read: “The n” (the retry limit parameter).
- Meaning: Caps retry loops—after invalid inputs, raise an exception instead of looping forever.
timeout=seconds: RaisesTimeoutExceptionif input takes too long.default='val': Instead of raising an exception, returns'val'if limit/timeout is reached.
- Regex Filtering:
allowRegexes=[r'pattern']: Specific strings that bypass other checks.blockRegexes=[r'pattern']: Specific strings that are explicitly forbidden.
inputCustom(func): Takes a custom function that raises an exception if validation fails.
import pyinputplus as pyip
# Simple integer validation with limit
response = pyip.inputInt("Enter a number: ", min=0, max=100, limit=3)
print(f"You entered: {response}")