为什么这个 Python 函数 return 会出现 None 错误?

Why does this Python function return a None error?

教科书A Beginner's Guide to Python 3,第11章有一个函数的例子。 程序是:

def get_integer_input(message):
    """
    This function will display the message to the user
    and request that they input an integer.

    If the user enters something that is not a number
    then the input will be rejected
    and an error message will be displayed.

    The user will then be asked to try again."""

    value_as_string = input(message)
    while not value_as_string.isnumeric():
        print("The input must be an integer greater than zero.")
        value_as_string = input(message)
        return int(value_as_string)


age = get_integer_input("Please input your age: ")
age = int(age)
print("age is", age)`

根据教科书的输出应该是:

Please input your age: 21
age is 21

但我得到:

Please input your age: 20

Traceback (most recent call last):
  File "/Users/RedHorseMain/Documents/myPythonScripts/A Beginners Guide to Python 3/6.10.3 getAge.py", line 20, in <module>
    age = int(age)
TypeError: int() argument must be a string, a bytes-like object or a number, not 'NoneType'

但是,如果我首先输入一个字符串而不是一个整数,函数应该防止的错误,它起作用了:

Please input your age: Red

The input must be an integer greater than zero.

Please input your age: 21

age is 21

有人可以解释为什么函数返回 'NoneType' 吗?

当您第一次尝试输入整数时,您不会进入 while 循环(因为条件永远不会满足),因此您不会到达该循环内的 return。你应该把这个 return 放在循环之外:

def get_integer_input(message):
    value_as_string = input(message)
    while not value_as_string.isnumeric():
        print("The input must be an integer greater than zero.")
        value_as_string = input(message)
    return int(value_as_string)

您已将 return 放入循环中,如果您输入 20 之类的数字,则永远不会进入该循环。 所以:

def get_integer_input(message):
    value_as_string = input(message)
    while not value_as_string.isnumeric():
        print("The input must be an integer greater than zero.")
        value_as_string = input(message)
        print(value_as_string)

    return int(value_as_string)
age = get_integer_input("Please input your age: ")
age = int(age)
print("age is", age)

一个简单的修复:

def get_integer_input(message):
    """
    This function will display the message to the user
    and request that they input an integer.

    If the user enters something that is not a number
    then the input will be rejected
    and an error message will be displayed.

    The user will then be asked to try again."""

    value_as_string = input(message)
    print(value_as_string)
    while not value_as_string.isnumeric():
        print("The input must be an integer greater than zero.")
        value_as_string = input(message)
        return int(value_as_string)
    return int(value_as_string)


age = get_integer_input("Please input your age: ")
age = int(age)
print("age is", age)

get_integer_input 中的 while 循环下方添加了 return 值,否则它实际上不会 return 任何东西,因为 value_as_string 在检查时算作数字while 循环,因此不为假,因此 while 循环永远不会开始,并且由于默认情况下它看不到其他语句,因此 age = get_integer_input("Please input your age: ") 返回一个 NoneType,当它尝试将其解析为 int 时:age = int(age) 它会抛出错误,因为您无法将 None 转换为可用数字。

因此,在 while 循环之外放置一个 return 值可以解决问题,因为现在函数 returning 一个值而不是默认为none 就没有 return.