错误输入后,函数返回None

时间:2017-01-04 10:28:41

标签: python python-3.x

我需要一个能够排除错误输入并再次要求输入的功能。但在输入错误后,它会返回None而不是新输入。我的代码有什么问题,我该如何解决?

def start():

    def inputNumber(answer):
        try:
            number = int(input(answer))
            if number <= 100 and number >= 0:
                print('%%%',number,'%%%')
                return number
            else:
                inputNumber('Number is wrong, please input number from 0 to 100: ')
        except (ValueError):
            inputNumber('It is not a number, please input number from 0 to 100: ')

    def checkInput(number2):
        print('$$$',number2,'$$$')
        if number < 50:
            return number2
        else:
            return checkInput(inputNumber('Input number from 0 to 100: '))

    number = 0
    print('###',checkInput(inputNumber('Input number from 0 to 100: ')),'###')
    start()

start()

结果如下:

  

输入数字0到100:777

     

数字错误,请输入0到100之间的数字:sadf

     

不是数字,请输入0到100:17之间的数字

     

%%% 17 %%%

     

$$$无$$$

     

TypeError:unorderable类型:NoneType()&lt; INT()

3 个答案:

答案 0 :(得分:4)

您正在递归地调用inputNumber但不会返回递归调用的结果。更好地使用循环而不是递归:

def inputNumber(prompt):
    while True:
        try:
            number = int(input(prompt))
            if 0 <= number <= 100:
                print('%%%',number,'%%%')
                return number
            prompt = 'Number is wrong, please input number from 0 to 100: '
        except ValueError:
            prompt = 'It is not a number, please input number from 0 to 100: '

顺便说一句:您还应该在其他功能中使用循环,而不是定义嵌套函数。

答案 1 :(得分:0)

问题是,一旦检查失败,您再次致电inputNumber,但不对答案做任何事情。你需要退货。

def start():

    def inputNumber(answer):
        try:
            number = int(input(answer))
            if number <= 100 and number >= 0:
                print('%%%', number, '%%%')
                return number
            else:
                return inputNumber('Number is wrong, please input number from 0 to 100: ')
        except (ValueError):
            return inputNumber('It is not a number, please input number from 0 to 100: ')

    def checkInput(number2):
        print('$$$', number2, '$$$')
        if number < 50:
            return number2
        else:
            return checkInput(inputNumber('Input number from 0 to 100: '))

    number = 0
    print('###', checkInput(inputNumber('Input number from 0 to 100: ')), '###')
    start()

start()

答案 2 :(得分:0)

运行此代码它将解决您的问题。你只需要捕获额外的错误(NameError

def start():

    def inputNumber(answer):
        try:
            number = int(input(answer))
            if number <= 100 and number >= 0:
                print('%%%',number,'%%%')
                return number
            else:
                inputNumber('Number is wrong, please input number from 0 to 100: ')
        except (ValueError, NameError) as e:
            inputNumber('It is not a number, please input number from 0 to 100: ')

    def checkInput(number2):
        print('$$$',number2,'$$$')
        if number < 50:
            return number2
        else:
            return checkInput(inputNumber('Input number from 0 to 100: '))

    number = 0
    print('###',checkInput(inputNumber('Input number from 0 to 100: ')),'###')
    start()

start()