【问题标题】:Python input validation for integers greater than zero大于零的整数的 Python 输入验证
【发布时间】:2020-04-15 01:12:57
【问题描述】:

我有以下代码,我试图验证用户提供的输入是否大于零,但返回的值不是正确的值 - 例如,如果我第一次输入零角度,然后输入大于零的值,返回的是零而不是正确的值。关于如何解决的任何想法?

def ValidTriangle(firstAngle, secondAngle, thirdAngle):

    if (firstAngle + secondAngle + thirdAngle) == 180:
        print("The triangle is indeed a valid triangle")
    else:
        print("The triangle is not valid")


def ValidateInput(angle):
        if angle <=0: 
            isValid = False
        else:
            isValid = True

        while isValid == False:
            try:
                angle = int(input('Please try again'))
                break

            except ValueError:
                return(angle)                

angle1 = int(input("Please enter the first angle of the triangle: \n"))
ValidateInput(angle1)
angle2 = int(input("Please enter the second angle of the triangle: \n"))
ValidateInput(angle2)
angle3 = int(input("Please enter the third angle of the triangle: \n"))
ValidateInput(angle3)

ValidTriangle(angle1,angle2,angle3)

【问题讨论】:

  • ValidateInput 仅在出现错误时返回;没关系,因为您不会对返回的任何值做任何事情。
  • 变量和函数名应该遵循lower_case_with_underscores风格。

标签: python validation input


【解决方案1】:

虽然我不完全确定在这种情况下接受和验证输入的最佳方法是什么,但这种解决方案应该是非常惯用和明智的。

def is_valid_triangle(angle_1: float, angle_2: float, angle_3: float) -> bool:
    return angle_1 + angle_2 + angle_3 == 180


def get_gt_zero_input(prompt_msg: str) -> int:
    while True:
        raw_in = input(prompt_msg)
        try:
            int_val = int(raw_in)
        except ValueError:
            input(f'ERROR: Invalid input: {raw_in}.\nPress ENTER to continue.')
        else:
            if int_val > 0:
                return int_val
            else:
                input(f'ERROR: Input {int_val} is smaller than or equal to 0.\nPress ENTER to continue.')

【讨论】:

    【解决方案2】:

    确保 ValidateInput 返回修改后的角度。这里的while循环也可以用递归替换,再次调用ValidateInput:

    def ValidTriangle(firstAngle, secondAngle, thirdAngle):
    
        if (firstAngle + secondAngle + thirdAngle) == 180:
            print("The triangle is indeed a valid triangle")
        else:
            print("The triangle is not valid")
    
    
    def ValidateInput(prompt):
        angle = int(input(prompt))
        if angle <= 0:
            isValid = False
        else:
            isValid = True
    
        if not isValid:
            angle = ValidateInput('Please try again: ')
    
        return angle            
    
    angle1 = ValidateInput("Please enter the first angle of the triangle: \n")
    angle2 = ValidateInput("Please enter the second angle of the triangle: \n")
    angle3 = ValidateInput("Please enter the third angle of the triangle: \n")
    
    ValidTriangle(angle1,angle2,angle3)
    

    【讨论】:

    • 这里的while循环也可以用递归替换,再次调用ValidateInput 为什么...?在所有可以做出的改变中......
    • 此外,此解决方案不会复制 OP 程序的功能,因为没有检查输入是否实际上是有效的 int,
    【解决方案3】:

    您忘记在条件 IsValid 为 True 并且在破坏 while 子句之后放入返回角子句。您应该添加它,它应该可以工作。即

    def ValidateInput(angle):
        if angle <=0: 
            isValid = False
        else:
            isValid = True 
            return(angle)
    
        _angle = angle
        while isValid == False:
            try:
                _angle = int(input('Please try again'))
                break
    
            except ValueError:
                return(_angle)
    

    你让它在错误时返回,而且你有一个被调用到函数中的变量来模仿在函数中所做的那个,这也令人困惑。我相信它应该返回一个不同的变量名以避免这种模仿。

    我可能没有解释清楚,但我希望对你有所帮助。

    【讨论】:

    • 当我尝试这种方式时,我仍然看到相同的结果 - 即只返回无效值
    • 您需要将 ValidateInput 的返回值存储到 angle1 angle1 = ValidateInput(angle1) 等每个 ValidateInput 调用中。这样,如果 ValidateInput 获得了角度的新值,您实际上是在存储它并在验证三角形时使用它。
    猜你喜欢
    • 1970-01-01
    • 2010-11-13
    • 2015-08-16
    • 2013-05-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多