Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
May 15, 2022 07:59 pm GMT

Validating Inputs in a Shorter Time

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 usingpip:

pipinstallpyinputplus

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() IscomparabletoinputChoice(),butinsteaddisplaysamenuwithnumberedorletteredchoices.

inputDatetime()
Ascertainsthattheuserhasenteredadateandtime.

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)
  1. Integer Input
import pyinputplus as pyip # integer input inp = pyip.inputInt(prompt = "Enter an Integer... ", default = 0, limit = 3) print(inp)
  1. 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?


Original Link: https://dev.to/okonkwomandy/validating-inputs-in-a-shorter-time-477c

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To