Python 验证

Python 验证

每次用户提交输入时,必须对其进行验证,看它是否与我们预期的完全一致。我们可以用两种方式之一来验证输入:通过应用一个标志变量或实现 try 或 except。标志变量最初将被设置为false,如果我们确定给定的数据与我们的期望值相同,我们将把标志状态改为true。从这里开始,我们可以根据标志状态来确定我们接下来可以做什么。如果结果是不利的,将执行 except 代码块。

Python中的验证类型

大体上我们可以把验证问题分为以下三类。

类型检查: 使用Python验证方法之一对提供的数据类型进行验证。Python的数据类型有int、float等。

长度检查: 使用任何一种描述的Python验证方法来验证所提供的输入字符串或任何可迭代的长度。

范围检查: Python的验证方法也可以实现,以确定所提供的输入数字是否在两个指定的数字之间。

下面我们描述了验证方法的语法。

使用标志的语法

# Syntax of using the flag variable to validate an input

flag = False
while not flag:
     if [put condition here]:
            flag = True
     else:
          print('Input is not Valid')

该标志的状态最初被设置为false,在一个while循环中考虑确切的条件,以防止语句变为true。如果所需的验证条件得到满足,该标志就被验证并设置为真;如果没有,就会产生一个错误信息。

# Syntax of using the try-except code block to validate an input

# Starting a while loop to always run this code
while True:
        # Starting a try block
        try:
            [Here, put the code which may contain an error]
       except:

print('This error message will be displayed if the code will have any error')

print('If the code written in the try block of the code above runs successfully, execute the code further from this point.')

我们首先将while循环的条件设置为真,然后,运行一个代码块来进行所需的验证。如果代码不能完成验证,将引发一个异常,显示错误信息。如果try代码块成功地完成了代码,会打印出一个成功的文本。

Python验证的例子

例子显示了上面描述的验证方法的实现。

例子-1

我们将检查输入的数据类型是否符合要求。

代码

# Python program to show how to use the flag variable to check the data type of the input variable

# Declaring a variable valid, which will work as a flag variable and will set it to False
valid = False

# Giving the flag variable as the condition of the while loop
# If the condition is true, it will prompt us to enter the input
while not valid:
    #The user will be prompted to give the input
    number = input('Please type a number: ')
    # We will check the input given by the user to see if it is of int data type
    if number.isdigit():
        #The flag variable is set to true if the condition in this if statement is true
        valid = True
    else:
        print('The input given is not a valid integer')
        # This will print this output if the input given by the user is of integer data type
    print('The given input is an integer and that is: ' + str(number))

输出

Please type a number: 8.8
The input given is not a valid integer
The given input is an integer and that is: 8.8

例子-2

这个Python程序使用一个标志变量验证用户提供的输入,看它是否在某个范围内。

代码

# Python program to show how to use the flag variable to check the range of the input variable lies in the specified range

# Declaring a variable valid, which will work as a flag variable and will set it to False
valid = False

# Giving the flag variable as the condition of the while loop
# If the condition is true, it will prompt us to enter the input
while not valid:
    # The user will be prompted to give the input
    number = int(input('Please type a number: '))
    # We will check the input given by the user to see if it lies in the specified range
    if number >= 13 and number <= 19:
        valid = True
    else:
      print('The number given by you does not lie between 13 and 19')
    # This output will be printed if the input given by the user lies between the specified range
    print('The number given by the user lies between 13 and 19 and the number given by you is: ' + str(number))

输出

Please type a number: 7
The number given by you does not lie between 13 and 19
The number given by the user lies between 13 and 19 and the number given by you is: 7

例子-3

该Python程序使用flag变量来确定输入字符串的长度。

代码

# Python program to show how to use the flag variable to check the range of the input variable

# Declaring a variable valid, which will work as a flag variable and will set it to False
valid = False

# Giving the flag variable as the condition of the while loop
# If the condition is true, it will prompt us to enter the input
while not valid:
    #The user will be prompted to give the input
    string = input('Please give a string: ')
    # We will check the input given by the user to see if it has a length greater than or equal to 5
    if len(string) >= 5:
        #The flag variable is set to true if the condition in this if statement is true
        valid = True
    else:
        print('The input string given has length less than 5')
        #This output will be printed if the input string given by the user has a specified length
    print('The given input string has a length greater than or equal to 5, and the string is: ' + string)

输出

Please give a string: Python
The given input string has a length greater than or equal to 5, and the string is: Python

效益

  • 它有助于提高代码的安全性。
  • Python的验证功能可以阻止外部用户错误地或有目的地误用代码。
  • 我们可以用它来确定给定的数据类型是否有效。
  • 我们可以用它来确定提供的输入是否有任何不正确的值。
  • 我们可以用它来确定一个输入是在范围内还是范围外。
  • 我们可以用它来确定所提供的输入是否符合对它的限制。
  • 我们可以用它来确定输入是否是一致的。
  • 我们可以使用 Python 的验证功能来确定一个输入是否合法。
  • 我们可以使用 Python 的验证功能来确定所提供的输入变量是否完整。

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程