【问题标题】:Why does this Python function return a None error?为什么这个 Python 函数返回 None 错误?
【发布时间】:2020-04-29 10:03:10
【问题描述】:

在教科书 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”吗?

【问题讨论】:

  • 你确定书中的函数实际上没有return 去凹层吗? (例如 - 它实际上不在 while 循环本身中......)

标签: python function nonetype


【解决方案1】:

一个简单的修复:

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 循环下方添加了返回值,否则它实际上永远不会返回任何内容,因为 value_as_string 在由该 while 循环检查时算作数字,因此不是假的,因此 while 循环永远不会开始并且因为默认情况下它看不到其他语句,所以age = get_integer_input("Please input your age: ") 会返回 NoneType,当它尝试将其解析为 int:age = int(age) 它会引发错误,因为您无法将 None 转换为可用的数字。

因此,在 while 循环之外放置一个返回值解决了这个问题,因为现在函数 is 返回一个值,而不是在不返回时默认为 none。

【讨论】:

  • 非常感谢,它现在可以正常工作了。并感谢您的解释!
【解决方案2】:

当您第一次尝试输入整数时,您不会进入 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)

【讨论】:

    【解决方案3】:

    您已将 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)
    

    【讨论】:

    • 非常感谢!它现在应该正常工作。教科书将“返回”放在循环中,我无法解决错误。这是迄今为止教科书中的第三个错误——其他两个对我来说更容易找到——所以也许我用错书了!
    猜你喜欢
    • 2013-02-17
    • 2023-04-09
    • 2023-01-01
    • 2021-12-23
    • 2019-11-23
    • 2023-03-11
    • 1970-01-01
    相关资源
    最近更新 更多