8 Input Validation Flashcards

1
Q

1.What is an alternative to:
input()
2. Why is it useful?
3. How can you import it with a shorter name?

A
  1. PyInputPlus
  2. It allows pre-configured input validation, e.g. accepting only numbers and giving feedback if the criteria is not met
  3. import pyinputplus as pyip
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is the standard structure of the different pyInputPlus methods?
Name 3x prompt examples
Name 3x prompt parameters

A

Structure:
»>input*(‘message’, ‘parameter for specification’)

Prompts:
»>inputDate()
»>inputStr()
»>inputInt()

Specification:
»>inputInt(‘Enter your age: ‘, min=1)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How can you display multiple options?
A. Selecting by value
B. Selecting by Number

A
A.
>>>response = 
pyip.inputMenu(['cat', 'dog', 'mouse'])
  * cat
  * dog
  * mouse
B. (['cat', 'dog', 'mouse'], numbered=True)
  1. cat
  2. dog
  3. mouse
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What command gives an overview over common input*() Parameters?

A

> > > help(pyip.paramters)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How can you customize input validation?

A
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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

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?

A

> > > response = pyip.inputNum(blockRegexes=[r’[135]$]’)

Equivalent:
allowRegexes=r[…]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Name the 4 parameters that can be used with pyip.inputNum(‘message’, parameter)

A
parameter:
min=X
max=X
greaterThan=X
lessThan=Y
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Does pyip.input*() allow blank input?

A

The default is blank=False

Setting it to blank=True in the Parameter enables it.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q
  1. How can you change how often/ long the user is asked for input?
  2. How do you change the return value if the input is wrong?
A
  1. pyipInput(timeout=10)
    –> 10 seconds
    pyipInput
    (limit=2)
    –> 2 tries
  2. pyipInput*(default=’message’)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly