我该如何解决:TypeError:unorderable类型:NoneType()<浮动()

时间:2016-01-23 18:18:06

标签: python-3.x unicode byte

我试图让这段代码在Python-3.x中运行

这是显示该行的部分:

    #Here we check to make sure horizon (19) and ovveride (16) digital pins aren't on
#print("GPIO 16 (ovveride) is " + str(GPIO.input(16)))
#print("GPIO 19 (horizon) is " + str(GPIO.input(19)))
#print(GPIO.input(19))
if (GPIO.input(16) == False) and (GPIO.input(19) == False): #check to see if the passive mode switch is on
# GPIO 16 is for override and GPIO 19 is for horizon mode

#In this section we rotate as needed
    starttime = datetime.datetime.utcnow()
    if (getcurheading() > getsolarheading()) and (getsolarangle() > 2) and (getcurheading() != 999):
        while (getcurheading() > (getsolarheading() + hmargin)) and (starttime + datetime.timedelta(seconds=pan_time_limit) > datetime.datetime.utcnow()):
            if debug == True:
                print("1: Moving " + str(getcurheading()) + " to " + str(getsolarheading()))
            motor2neg()
        motors.setSpeeds(0, 0)

starttime = datetime.datetime.utcnow()
if (getcurheading() < getsolarheading()) and (getsolarangle() > 2) and (getcurheading() != 999):
    while (getcurheading() < (getsolarheading() - hmargin)) and (starttime + datetime.timedelta(seconds=pan_time_limit) > datetime.datetime.utcnow()):
        if debug == True:
            print("2: Moving " + str(getcurheading()) + " to " + str(getsolarheading()))
        motor2pos()
    motors.setSpeeds(0, 0)

starttime = datetime.datetime.utcnow()
if (getcurheading() > tomorrow_static) and (getsolarangle()<0) and (getcurheading() != 999):
    if (getcurheading() - tomorrow_static) > sleep_tolerance:
        while (getcurheading() > (tomorrow_static + hmargin)) and (starttime + datetime.timedelta(seconds=pan_time_limit) > datetime.datetime.utcnow()):

生成的错误是:

Traceback (most recent call last):
  File "solarrobot7-core.py", line 261, in <module>
    if (getcurheading() < getsolarheading()) and (getsolarangle() > 2) and (getcurheading() != 999):
TypeError: unorderable types: NoneType() < float()

这是Unicode字符串与字节的另一种情况吗?

以下是我认为的目标定义。

#Translate the IMU from magnetic north to true north since the calcs use true north
def getcurheading():
# The escape character for # is \x23 in hex
    serialport.write(b"\x23o0 \x23f")
    headresponse = serialport.readline()
#   print(headresponse)
    words = headresponse.split(",")
    if len(words) > 2:
        try:
            curheading = (float(words[0])) + 180
            if curheading + Declination > 360: curheading = curheading - 360 + Declination
            else: curheading = curheading + Declination
        except:
            curheading = 999
#   print(curheading)
    return curheading

感谢您指出我没有包含定义。

1 个答案:

答案 0 :(得分:1)

if (getcurheading() < getsolarheading()) and (getsolarangle() > 2) and (getcurheading() != 999):
TypeError: unorderable types: NoneType() < float()

此错误表示您的getcurheading函数返回None,函数getsolarheading返回float,这就是为什么这些值的比较不适用

因此,我建议您调查getcurheading函数并考虑它何时返回None以及原因。或与我们分享此功能

更新:

正如我从函数的定义中看到的那样,您使用空格分隔符写入文件,并使用逗号分隔符进行读取,这就是words列表中少于2个项目的原因,作为结果,curheading变量未定义,函数返回None问题的根源是什么

如果您的文件中有类似内容:#o0 #f

words = headresponse.split(",")

单词将等于["#o0 #f'] 而且你永远不会进入这个循环:

if len(words) > 2:

要解决您的问题,您可以更改此行。替换这个:

words = headresponse.split(",")

用这个:

words = headresponse.split(" ")