如何检查输入的值??Python 3.4

2024-10-03 11:24:27 发布

您现在位置:Python中文网/ 问答频道 /正文

def p_parameter(p):

     if p < 10 or p > 100:
        int(input("Please try again: "))
        newp = p
        return newp
    else:
        newp <10 or newp >100
        input("Good bye")
        bye()

def main():

        speed(0)
        R=100
        r=4
        p=int(input("Please enter a number between 10 and 100: "))
        p_parameter(p)
        t_iter(R,r,p)
        Xcord(R,r,p,t_iter(R,r,p))
        Ycord(R,r,p,t_iter(R,r,p))
        input("Hit enter to close porgram")
        bye()

main()

这是我为一个绘制螺旋图的程序编写的代码。程序运行良好,但是我在这里发布的是我试图让用户为p输入一个介于10-100之间的值。在

我要做的是检查p是<;10还是p>;100。如果是,那么给用户一个机会重新输入一个新的值p,只要它符合允许的限制就使用它。在第二次检查用户是否仍然输入了不正确的值p之后,我希望程序关闭。在

问题是它检查“p”,然后再次请求p,但它只接受第一个“p”值,而不执行第二个检查或给p新值。在


Tags: or用户程序inputifparametermaindef
2条回答

你应该在函数中提示一下。这是编写此类验证的标准习惯用法:

def validate():
    while True:
        # begin inf. loop
        p = input("some prompt goes here")
        if some_validation:
            break
            # end inf. loop
        # therefore if the validation fails, it reprompts
    # once you're out of the inf. loop...
    return p

这是你可以建造一个很好的装饰工的东西之一

^{pr2}$

解决方案的问题和更正:

it only take the first value of p and does not give p its new value.

  1. 再次向用户请求p,但不要将这个新值赋给newp变量,如下所示:

    newp = int(input("Please try again: "))
    

does not preform the second check.

  1. 您的else语句正在检查newp变量范围之外的newp上的条件,该变量位于if语句内。您应该将检查newp变量封装在if语句中,如下所示:

    def p_parameter(p):
        if p < 10 or p > 100:
            newp = int(input("Please try again: "))
            if newp <10 or newp >100:
    
  2. 当程序没有输入if语句时,p_parameter()函数中没有return语句。所以应该是这样的:

    def p_parameter(p):
        if p < 10 or p > 100:
            newp = int(input("Please try again: "))
            if newp <10 or newp >100:
                print( "Good Bye")
                bye()
            else: 
                return newp    # need return statements like this
        else:
            return p           # need return statements like this
    

对您的问题的建议解决方案

What I want to do is check to see if 'p' is < 10 or 'p' > 100, if it is then give the user a chance to reenter a new value of 'p' and use that value as long as it fits within the allowed parameters.

  1. 如果收到正确答案,请使用while True:无限循环和break语句退出。在
  2. 使用tryexcept和{}块和{}

    1. 捕捉由非整数输入引起的任何错误。在
    2. 检测任何超出允许范围的输入。在

After the second check if the user still has entered an incorrect value of 'p' I want the program to close.

要关闭程序,您可以:

  1. 让用户点击ctrl+c(我的个人偏好)、
  2. 设置一个计数器,该计数器指示要运行while循环的次数,然后使用sys.exit(0)来强制程序退出,如果达到了一个限制(注意:首先您需要import sys才能使用它;我认为您的bye()就是这样做的)。在

把它们放在一起:

  1. main()函数中,删除input()语句,只调用p_parameter()函数,如下所示: p=p_参数()

  2. 按如下方式定义p_parameter()函数:

    import sys
    def p_parameter():
        exit_counter = 1    # counter to exit after 2 tries
    
        while True:
            try:
                p = int( input( "Please enter a number between 10 and 100: ") )
            except ValueError:
                print( "Not a number, Try Again" )
            else:
                if 10 < p < 100:    # this is much faster than your approach
                    break           # exit the loop
                else: 
                    print( "Value not in range")
    
                    # increment counter to exit when 2 tries are over
                    exit_counter+=1
                    if ( counter > 2 ):
                        print( "Good Bye" )
                        sys.exit(0)  # bye()
    
         return p
    

相关问题 更多 >