简体   繁体   English

为什么这个 Python function 返回 None 错误?

[英]Why does this Python function return a None error?

In the textbook A Beginner's Guide to Python 3, there is an example of a function in Chapter 11. The program is:在教科书A Beginner's Guide to Python 3中,第11章有一个function的例子。程序是:

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)`

The output, according to the textbook, should be: output,根据教科书应该是:

Please input your age: 21
age is 21

But I get:但我得到:

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'

However, if I first enter a string instead of an integer, the error that the function is supposed to protect against, it works:但是,如果我首先输入一个字符串而不是 integer,function 应该防止的错误,它可以工作:

Please input your age: Red

The input must be an integer greater than zero.

Please input your age: 21

age is 21

Would someone please explain why the function is returning a 'NoneType'?有人可以解释为什么 function 返回“NoneType”吗?

When you enter an integer at the first try, you don't enter the while loop (because the condition is never satisfied), so you don't reach return which is within that loop.当您在第一次尝试时输入 integer 时,您不会进入 while 循环(因为条件永远不会满足),因此您不会到达该循环内的return You should have put this return outside the loop:你应该把这个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)

You have put the return inside the loop, which is never entered if you input a number like 20. So:您已将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)

A simple fix:一个简单的修复:

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)

Added the return value just beneath the while loop in get_integer_input as otherwise it never actually returns anything because the value_as_string counts as numeric when checked by that while loop and thus is not false and so the while loop never begins and since it sees no other statements by default the age = get_integer_input("Please input your age: ") gets a NoneType back and when it attempts to resolve that to an int: age = int(age) it throws an error as you can't convert None into a usable number.get_integer_input中的 while 循环下方添加了返回值,否则它实际上永远不会返回任何内容,因为value_as_string在由该 while 循环检查时算作数字,因此不是假的,因此 while 循环永远不会开始,因为它看不到其他语句默认age = get_integer_input("Please input your age: ")返回 NoneType ,当它尝试将其解析为 int 时: age = int(age)会引发错误,因为您无法将 None 转换为可用的数字.

As such putting a return value outside of that while loop solves the issue as now the function is returning a value instead of defaulting to none on a no return.因此,将返回值放在 while 循环之外解决了这个问题,因为现在 function正在返回一个值,而不是在不返回时默认为 none。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM