2014-12-01 47 views
-2

我們試圖找出如何比較兩個非類型的值來找到最高得分的骰子,但是每次運行代碼時都會顯示Typeerror:unorderable types:Nonetype()> Nonetype( )返回最高的兩個NoneTypes

def compareresult(): 
    if giveresult(dice) > giveresult(newdice): 
     print(giveresult(dice)) 

    elif giveresult(newdice) > giveresult(dice): 
     print(giveresult(newdice)) 
    return dice, newdice 

giveresult是:

def giveresult(tempDice): 
    if fiveofakind(tempDice) is True: 
     tempScore = int(50) 
     print(tempScore) 
    if fiveofakind(tempDice) is False: 
     tempScore = int(0) 
     print(tempScore) 
+1

你不能。您試圖將'None'與'None'進行比較,因爲'giveresult'不會返回任何其他內容。你需要修復你的'giveresult()'函數來*返回一個值。 – 2014-12-01 17:02:27

+0

向我們展示'giveresult()'的定義。 – 2014-12-01 17:03:06

+0

這是怎麼做到的? – BinaryBoy 2014-12-01 17:03:08

回答

0

giveresult()功能不返回任何東西,所以返回的默認None。打印不是一回事;您正在向終端寫入文本,而不是返回,並且調用者無法使用寫入終端的文本。

return更換print()

def giveResult(tempDice): 
    if fiveofakind(tempDice): 
     return 10 
    else: 
     return 0 

我還簡化了您的功能;沒有必要測試is True; if已經測試fiveofakind()的結果是否爲真。由於您已經測試過fiveofakind(),所以您只需使用else就可以選擇其他情況。

下,避免調用giveResult不是每個骰子更多,並再次,從功能結果:

def compareresult(): 
    dice_result = giveResult(dice) 
    newdice_result = giveResult(newdice) 
    if dice_result > newdice_result 
     return dice_result 

    elif newdice_result > dice_result: 
     return newdice_result 

    return dice, newdice 

如果你必須返回較大的結果,只需使用max() function

def compareresult(): 
    dice_result = giveResult(dice) 
    newdice_result = giveResult(newdice) 
    return max(dice_result, newdice_result)