2013-11-22 26 views
0

我有平等問題。以下代碼將嵌套列表與列表骰子進行比較。Python - 平等錯誤

def largeStraight(dice): 
    straightValues = [{1, 2, 3, 4, 5}, {2, 3, 4, 5, 6}] 
    return any(value.issubset(dice) for value in straightValues) 

def smallStraight(dice): 
    straightValues = [{1, 2, 3, 4}, {2, 3, 4, 5} , {3 ,4, 5, 6}] 
    return any(value.issubset(dice) for value in straightValues) 

def giveResult(dice): 
    score = 0 
    if(largeStraight): 
     score = 40 
    elif(smallStraight): 
     score = 30 
    else: 
     score = 0 
    return score 

dice = [1,2,3,4,1] 
print(giveResult(dice)) 

這應返回的30從giveResult的值,但是我得到一個分數的40

+0

你需要做的第一件事是'largeStraight(骰子) '和'smallStraight(骰子)'。你不是通過擲骰子,而是評估一個函數的真實性,這是真實的。 – sberry

回答

3

您需要通話您的功能:

def giveResult(dice): 
    score = 0 
    if largeStraight(dice): 
     score = 40 
    elif smallStraight(dice): 
     score = 30 
    else: 
     score = 0 
    return score 

只是指的是函數對象意味着你的第一個if將匹配,因爲大多數Python對象在布爾上下文中被認爲是真的。

您可以提前返回,簡化你的函數一點:

def giveResult(dice): 
    if largeStraight(dice): 
     return 40 
    if smallStraight(dice): 
     return 30 
    return 0 
0

你不傳遞任何進入你的方法:

def giveResult(dice): 
    score = 0 
    if(largeStraight(dice)): 
     score = 40 
    elif(smallStraight(dice)): 
     score = 30 
    else: 
     score = 0 
    return score