2016-11-04 23 views

回答

1

首先,第二個「return 0」應該是「return 10」。

其次,(這可能會也可能沒有影響),您可能需要在elif行中使用圓括號。

def green_ticket_value(a, b, c): 
    if a != b or b != c or a != c: 
     return 0 
    elif (a == b and a != c) or (b == c and b != a) or (a == c and a!= b): 
     return 10 # NOT 0 
    else: a == b == c 
     return 20 
+0

是的,我也在想,它肯定會讓他更清楚他想要做什麼(並確保這就是實際發生的事情)。值得注意的是,上述語句仍然是錯誤的,因爲a == c,b == a和a == b憑藉「a!= b或b!= c或a!= c」爲假。 – EJoshuaS

0

問題出在第一個if語句邏輯。您正在使用or而不是and這就是爲什麼您從不比較第二種說法。

假設在當前的代碼a=1b=2c=2 這是事實,A = B,因此是一個有效的語句,並返回0
它應該是:

def green_ticket_value(a, b, c): 
    if a != b and b != c and a != c: 
    #rest of code 

你也可以使用不同的方法是使用計數器並返回最常見的值

from collections import Counter 

def green_ticket_value(*args): 
    green = Counter(args) 
    values = {1:0,2:10,3:20} 
    return values[green.most_common(1)[0][1]] 
0

想想這樣。如果

a != b or b != c or a != c 

是假的,那麼

a == b and b == c and a == c 

DeMorgan's Laws解釋爲什麼是這種情況。請記住,

~(A \/ B) <-> ~A /\ ~B 
~(A /\ B) <-> ~A \/ ~B 

<->基本意思是「彼此相等」,~的意思是「不」,\/手段「或」和/\手段「和」。

要經過這樣的代碼:

def green_ticket_value(a, b, c): 
    if a != b or b != c or a != c: 
     return 0 
    # It can *never* be the case that a != c or a != b at this point 
    elif a == b and a != c or b == c and b != a or a == c and a!= b: 
     return 0 // Should this be "return 10"? 
    # What did you mean to do here? 
    else: a == b == c 
     return 20 

這是一個有點不清楚你是否意味着

a == b and a != c or b == c and b != a or a == c and a!= b 

意味着

(a == b and a != c) or (b == c and b != a) or (a == c and a!= b) 

這將永遠是假的(A ==ç和a == b,這意味着所有這些條件都是錯誤的),或者t他以下幾點:

a == b and (a != c or b == c) and (b != a or a == c) and a!= b 

這也是始終爲假(其實,這是自相矛盾的地方,因爲它包含a == b && a != b),或者如果你的意思是一些其他的變種。我會強烈建議在這裏使用parenthases更清楚你想要做什麼。

此外,對於最後一部分:

(b == c and b != a) or (a == c and a != b) 

a != bb != a意味着同樣的事情,因此,上述表態意味着同樣的事情

a != b and (a == c or b == c)