输入验证

时间:2016-04-19 12:51:46

标签: validation python-3.x

我希望用户根据这些条件输入。 1 - 用户必须给(不允许空白输入) 2 - 它必须是整数且大于或等于零(我的意思是不是负数) 我尝试了很多方法。但我还没有找到解决方案。

def check():

        value = []
        price = 0
        ok = False
        rece = []
        noinput = False

        valid = False
        while valid == False:
            #try:
            #    inp = input("Price per day: $")
            #except SyntaxError:
            #    inp = None
            nonInpCheck = False
            while nonInpCheck == True:
                try:
                    inp = input("Price Per day: $")
                except:
                    if not inp:
                        raise ValueError('Please put any data')
                        nonInpCheck = False
                    else:
                        nonInpCheck = True
            while True:
                try:
                    int(inp)
                except ValueError:
                    inp = input ("Invalid input: please type numeric value")
                    continue
                else:
                    break

            print(type(inp))

            itisnotNegative = False
            if inp < 0:
                print ("Price must be >= $0 \n Invalid input: enter a valid number")
                itisnotNegative = False
            else:
                itisnotNegative = True
                price = inp
                valid = True




        value.append(ok)
        value.append(price)
        return value
ok = False
    val = []
    val = check()
    while val[0]==False:
        val = check()
    data = [0,name,val[1],'y']

有什么建议吗? 感谢

0 个答案:

没有答案