Python - TypeError - TypeError:“NoneType”和“int”的实例之间不支持“<”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43708541/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 23:17:59  来源:igfitidea点击:

Python - TypeError - TypeError: '<' not supported between instances of 'NoneType' and 'int'

python

提问by Dino3GL

TypeError: '<' not supported between instances of 'NoneType' and 'int'

类型错误:“NoneType”和“int”的实例之间不支持“<”

I have looked for an answer in Stack Overflow and found that I should be taking an int(input(prompt)), but that's what I amdoing

我在 Stack Overflow 中寻找答案,发现我应该使用 int(input(prompt)),但这就是我正在做的

def main():      
while True:
        vPopSize = validinput("Population Size: ")
        if vPopSize < 4:
            print("Value too small, should be > 3")
            continue
        else:
            break

def validinput(prompt):
while True:
    try:
        vPopSize = int(input(prompt))
    except ValueError:
        print("Invalid Entry - try again")
        continue
    else:
        break

回答by MartyMacGyver

This problem also comes up when migrating to Python 3.

迁移到 Python 3 时也会出现这个问题。

In Python 2 comparing an integer to Nonewill "work," such that Noneis considered less than any integer, even negative ones:

在 Python 2 中,比较一个整数None将“有效”,这样None被认为小于任何整数,甚至是负整数:

>>> None > 1
False
>>> None < 1
True

In Python 3 such comparisons raise a TypeError:

在 Python 3 中,这样的比较会引发一个TypeError

>>> None > 1
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: '>' not supported between instances of 'NoneType' and 'int'

>>> None < 1
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: '<' not supported between instances of 'NoneType' and 'int'

回答by Copperfield

you need to add a return in your function to get the number you input, otherwise it return an implicit None

你需要在你的函数中添加一个 return 来获取你输入的数字,否则它返回一个隐式 None

def validinput(prompt):
    while True:
        try:
            return int(input(prompt)) 
            # there is no need to use another variable here, just return the conversion, 
            # if it fail it will try again because it is inside this infinite loop
        except ValueError:
            print("Invalid Entry - try again")


def main():      
    while True:
        vPopSize = validinput("Population Size: ")
        if vPopSize < 4:
            print("Value too small, should be > 3")
            continue
        else:
            break

or as noted in the comments, make validinput also check if it is an appropriate value

或者如评论中所述,使 validinput 也检查它是否是合适的值

def validinput(prompt):
    while True:
        try:
            value = int(input(prompt)) 
            if value > 3:
                return value
            else:
                print("Value too small, should be > 3")
        except ValueError:
            print("Invalid Entry - try again")


def main():      
    vPopSize = validinput("Population Size: ")
    # do stuff with vPopSize

回答by Johan Steunenberg

Try: 
    def validinput(prompt):
    print(prompt) # this one is new!!
    while True:
        try:
            vPopSize = int(input(prompt))
        except ValueError:
            print("Invalid Entry - try again")
            continue
        else:
            break

And you will notice when the function is called.

您会注意到函数何时被调用。

The problem is that validinput() does not return anything. You'd have to return vPopSize

问题是 validinput() 不返回任何内容。你必须返回 vPopSize