Validating Inputs in a Shorter Time
Tags: python, beginners, programming, codenewbie
Published: 2022-05-15 19:59:30

Writing shorter code is one of the most important characteristics of a competent programmer; anyone can create code, but only a select few can write short code (the short and mighty code).

Let's have a look at this code.

```
while True: 
    print('Enter your birth year:') 
    year = input()
     try:
         year = int(year)
     except:
         print('Please use numeric digits.')
         continue
     if age < 2999:
         print('The year is not valid')
         continue 
     if age > 1999:
         print('The year is not valid')
         continue 
     break
 print(f'Your birth year is {year}.')
```

Another chunk of code to examine.

```
age = pyip.inputInt('Enter a new num: ', max=2999, min=1999 )

```

Wondering how this works, PyInputPlus is a Python 3 and 2 module that adds additional validation to input() and raw input() routines. Al Sweigart is the creator and maintainer of PyInputPlus. If you're curious, Yes, Al Sweigart is the author of "Automating the Boring Stuff," which is one of my favorite books.

PyInputPlus can be installed from PyPI using pip:

`pip install pyinputplus`

And then you can run

`Import pyinputplus as pyip`

###What else am I able to do with this?

**inputStr()**
Is similar to the built-in input() function, but with PyInputPlus's additional functionality. You can also use it to call a custom validation function.

**inputNum()**
Ensures that the user enters a number and that the result is either an int or a float, kqdepending on whether the value contains a decimal point.

**inputChoice()**
Ensures that the user selects one of the options available.

**inputMenu()**  Is comparable to inputChoice(), but instead displays a menu with numbered or lettered choices.

**inputDatetime()**
Ascertains that the user has entered a date and time.

**inputYesNo()**
Ensures that the user responds with a "yes" or "no"

**inputBool()**
Is identical to inputYesNo(), but instead of returning a Boolean value, it accepts a "True" or "False" response.

**inputEmail()**
Ascertains if the user has entered a genuine email address.

**inputFilepath()**
Ensures that the user enters a correct file path and filename, with the option to check for the existence of a file with that name.

**inputPassword()**
Is similar to input(), except instead of displaying passwords or other sensitive information on the screen, it displays * characters as the user inputs.

### How can I use this in my project

1. **String Input**
```
import pyinputplus as pyip 

# string input 

inp = pyip.inputStr(prompt="Enter a string... l",  blank=True) 

print(inp)

```

2. **Integer Input**
```
import pyinputplus as pyip 

# integer input 

inp = pyip.inputInt(prompt = "Enter an Integer... ",  

                    default = 0, limit = 3) 

print(inp)
```

3. **Menu Input**
```
import pyinputplus as pyip 

# menu item input 

inp = pyip.inputMenu(['apple', 'orange', 'mango']) 

print(inp)

```
###Why should I write shorter codes

Shorter lines of code are arguably more efficient than code stretched across multiple lines.

 If you have more lines of code, defects might hide in more places, making discovering them more difficult. 

Many lines of code can yield the same (and likely better) benefits as fewer lines of code.

Why not try writing shorter codes today?