如何解决:类型错误:unorderable类型:NoneType()<浮动()
我想要得到这个代码在Python的3.x的如何解决:类型错误:unorderable类型:NoneType()<浮动()
这是出现的线部分运行:
#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字符串与字节另一种情况?
下面是我认为是curheading的定义。
#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
感谢您指出我没有包含定义j。
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(" ")
@ frazelle09,我已经更新了我的答案 – pivanchy
猜猜是什么。经过进一步检查,我们发现这些线条没有正确缩进。我们固定他们,一切都是。我们正在检查这个500+程序中的下一行。谢谢你的帮助! – frazelle09
@ frazelle09不成问题。别客气! – pivanchy
什么'getcurheading'? – pivanchy
感谢您的回复,pvanchy!我会试一试并回复你。正如你所看到的,我已经在serialport.write(b“\ x23o0 \ x23f”)中引入了“b”,因为它将Unicode转换为字节错误。 – frazelle09
目前的问题是不相关为unicode的问题,它的东西,另一个 – pivanchy