8 Input Validation Flashcards
1.What is an alternative to:
input()
2. Why is it useful?
3. How can you import it with a shorter name?
- PyInputPlus
- It allows pre-configured input validation, e.g. accepting only numbers and giving feedback if the criteria is not met
- import pyinputplus as pyip
What is the standard structure of the different pyInputPlus methods?
Name 3x prompt examples
Name 3x prompt parameters
Structure:
»>input*(‘message’, ‘parameter for specification’)
Prompts:
»>inputDate()
»>inputStr()
»>inputInt()
Specification:
»>inputInt(‘Enter your age: ‘, min=1)
How can you display multiple options?
A. Selecting by value
B. Selecting by Number
A. >>>response = pyip.inputMenu(['cat', 'dog', 'mouse']) * cat * dog * mouse B. (['cat', 'dog', 'mouse'], numbered=True) 1. cat 2. dog 3. mouse
What command gives an overview over common input*() Parameters?
> > > help(pyip.paramters)
How can you customize input validation?
By using a function and >>>pyip.inputCustom(validFunc) e.g.: >>> def raiseIfUppercase(text): ... if text.isupper(): ... raise Exception('Input cannot be uppercase.') ... >>> inputCustom(raiseIfUppercase)
How can you use a regex with pyip?
Make it so that everything Number is blocked, that ends with a 1, 3 or 5
What is the equivalent?
> > > response = pyip.inputNum(blockRegexes=[r’[135]$]’)
Equivalent:
allowRegexes=r[…]
Name the 4 parameters that can be used with pyip.inputNum(‘message’, parameter)
parameter: min=X max=X greaterThan=X lessThan=Y
Does pyip.input*() allow blank input?
The default is blank=False
Setting it to blank=True in the Parameter enables it.
- How can you change how often/ long the user is asked for input?
- How do you change the return value if the input is wrong?
- pyipInput(timeout=10)
–> 10 seconds
pyipInput(limit=2)
–> 2 tries - pyipInput*(default=’message’)