Python - TypeError - TypeError:'<' 'NoneType'和'int'实例之间不支持

时间:2017-04-30 16:17:25

标签: python

  

TypeError:'<' 'NoneType'和'int'

的实例之间不支持

我在Stack Overflow中寻找答案,发现我应该接受一个int(输入(提示)),但这就是我做的事

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

4 个答案:

答案 0 :(得分:2)

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

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

或如评论中所述,使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

答案 1 :(得分:2)

when migrating to Python 3也会出现此问题。

在Python 2中,将整数与None进行比较将是有效的,因此None被认为小于任何整数,甚至是负整数:

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

在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'

答案 2 :(得分:0)

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

你会注意到调用该函数的时间。

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

答案 3 :(得分:0)

这是一个测试:)


from pylab import *
# Newtons symmetriske kvotient
def derivertsym (f , x , delta_x ) :
    fder = ( f ( x + delta_x ) - f (x - delta_x ) ) /(2* delta_x )
    return fder
# Newtons kvotient
def derivert (f , x , delta_x ) :
    fder = ( f ( x + delta_x ) - f ( x ) ) / delta_x
    return fder
# Rektangelmetoden fra boken med små endringer
def rektangelmetoden (f , a , b , n ) :
    total = 0.0
    h = (b - a ) / n
    for k in range (0 , n ) :
        total = total + f ( a + ( k * h ) )
    Areal = h * total
    return Areal