2016-01-23 160 views
0

我想要得到這個代碼在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。

+0

什麼'getcurheading'? – pivanchy

+0

感謝您的回覆,pvanchy!我會試一試並回復你。正如你所看到的,我已經在serialport.write(b「\ x23o0 \ x23f」)中引入了「b」,因爲它將Unicode轉換爲字節錯誤。 – frazelle09

+0

目前的問題是不相關爲unicode的問題,它的東西,另一個 – pivanchy

回答

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(" ") 
+0

@ frazelle09,我已經更新了我的答案 – pivanchy

+0

猜猜是什麼。經過進一步檢查,我們發現這些線條沒有正確縮進。我們固定他們,一切都是。我們正在檢查這個500+程序中的下一行。謝謝你的幫助! – frazelle09

+0

@ frazelle09不成問題。別客氣! – pivanchy

相關問題