2014-05-16 26 views
1

我想使這個程序,我想從它隨機得到生成的列表中找到2,3,4,...,K,A,如果有多於一個編號n返回該號碼。同樣的第三高清,但我想要的C,D,H,S,然後返回它們有多少。但是我從第二場比賽中得到的所有結果都沒有。我需要修正什麼才能使其工作?Python中的列表數字字符

這裏是代碼,如果它可以幫助任何人

import random 
def make_deck(): 
    suits=['C', 'D', 'H', 'S'] 
    ranks=['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A'] 
    deck = [s+r for s in suits for r in ranks] 
    random.shuffle(deck) 
    return deck 


def same_rank(hand, n): 
    if hand.count('2')>n: 
     count2=hand.count(2) 
     return count2 
    else: 
     return 'None' 
    if hand.count(3)>n: 
     count3=hand.count(3) 
     return count3 
    else: 
     return 'None' 
    if hand.count(4)>n: 
     count4=hand.count(4) 
     return count4 
    else: 
     None 
    if hand.count(5)>n: 
     count5=hand.count(5) 
     return count5 
    else: 
     return 'None' 
    if hand.count(6)>n: 
     count6=hand.count(6) 
     return count6 
    else: 
     return 'None' 
    if hand.count(7)>n: 
     count7=hand.count(7) 
     return count7 
    else: 
     return 'None' 
    if hand.count(8)>n: 
     count8=hand.count(8) 
     return count8 
    else: 
     return 'None' 
    if hand.count(9)>n: 
     count9=hand.count(9) 
     return count9 
    else: 
     return 'None' 
    if hand.count(10)>n: 
     count10=hand.count(10) 
     return count10 
    else: 
     return 'None' 
    if hand.count('J')>n: 
     countJ=hand.count('J') 
     return countJ 
    else: 
     return 'None' 
    if hand.count('J')>n: 
     countQ=hand.count('Q') 
     return countQ 
    else: 
     return 'None' 
    if hand.count('K')>n: 
     countK=hand.count('K') 
     return countK 
    else: 
     return 'None' 
    if hand.count('A')>n: 
     countA=hand.count('A') 
     return countA 
    else: 
     return 'None' 


def same_suit(hand): 
    if hand.count('C')>0: 
     countC=hand.count('C') 
    if hand.count('D')>0: 
     countD=hand.count('D') 
    if hand.count('H')>0: 
     countH=hand.count('H') 
    if hand.count('S')>0: 
     countS=hand.count('S') 
    return countC, countD, countH, countS 


hand = make_deck()[:10] 
print hand 
n=raw_input('Give n') 
print same_rank(hand,n) 

回答

1

這裏有一個問題:

def same_rank(hand, n): 
    if hand.count('2')>n: 
     count2=hand.count(2) 
     return count2 
    else: 
     return 'None' 
    #... 
    #Nothing here will matter! 
    #... 

在你的第一個if情況下,兩兩件事情將要發生。函數將返回count2或返回'None'。一旦函數返回,它就是完成並且該函數中不會再有代碼運行。所以其他if/else案件都不重要,因爲第一個if/else處理所有可能性,然後終止。

你想要的結構更像是:

if condition 1: 
    ... 
elif condition 2: 
    ... 
... 
elif condition10: 
    ... 
else: 
    return None 

編輯:也有代碼其他編程/邏輯錯誤。你的一個案例說

else: 
    None 

,它實際上並不做任何事情。

返回字符串'None'而不是python空對象None也可能是一個錯誤。

+0

我把所有的人,如果因爲我想檢查所有的人,我知道,如果他們中的一個是事實,就不會再繼續檢查,如果別人的真實,是有機會的一個下一個也是如此。 – RedRose23

+0

@ RedRose23問題不在於一旦一個成立就停止檢查其他人。無論如何,它在第一次檢查後停止檢查!無論'hand.count('2')> n'由於第一個'else'情況是真還是假,函數都會返回'return'。 – turbulencetoo

+0

我做了你說的改動,加上我改進了代碼,所以現在就像你說的只是如果因爲我不能讓它檢查所有與elif(我沒有得到任何錯誤),現在唯一的問題是它最終不會返回,我不希望那樣。有什麼辦法可以刪除它嗎? (我把它放在前面,我也刪除它,但它是一樣的,即使我擦除其他東西仍然返回無) – RedRose23

0
# to count ranks, rank should be a string: 
def rank_counter(hand, rank): 
    return sum(1 for card in hand if card[1:] == rank) 

# finds the first rank that has at least n cards in hand 
def same_rank(hand, n): 
    ranks = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A'] 
    for rank in ranks: 
     count = rank_counter(hand, rank) 
     if count > n: return count 
    return None 

# also create an integer for n: 
print same_rank(hand, int(n)) 
+0

這看起來不錯,但也許一些聰明的一些解釋會使它更有幫助。 –

0

有很多問題,你same_rank()功能:

  1. 一個if語句的條件爲if hand.count('2'),但另一個if語句的條件是if hand.count(3)
  2. hand是一個字符串列表,而不是一個字符串,所以試圖統計'2'出現的次數總是爲0,因爲你的卡片也有套裝。
  3. 當您認爲您的意思是return None時,您曾輸入過None
  4. 這可能不是問題,但您返回'None'而不是None
  5. 代碼將始終卡住第一個if/else塊,因爲if和else子句中都有返回語句。其他if/else塊具有相同的問題。

現在要修復你的代碼,取出所有其他子句,並在末尾放置一個return None。這意味着只有在所有其他條件都失敗的情況下,您的功能纔會返回None。然後重新考慮你將如何計算手牌中具有相同等級的牌的數量。然後考慮如何使用for循環而不是13 if語句。一旦這些事情已經做了,代碼可能會是這樣的:

def same_rank(hand, n): 
    for rank in list('23456789JQKA').append('10'): # compact version of ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A'] 
     count = 0 
     for card in hand: 
      if card[1:] == rank: 
       count += 1 
     if count > n: 
      return count 
    return None 

same_suit()功能有一些問題:如有的if語句失敗,其中一個變量返回末將不會被定義。你也不需要那麼多的陳述,而當你試圖計算這些牌的套數時,它將不起作用(上面第2點中提到的理由)。工作版本:

def same_suit(hand): 
    counts = {'C': 0, 'D': 0, 'H': 0, 'S': 0} # dictionary that fixes your problem of undefined variables 
    for suit in 'CDHS': 
     for card in hand: 
      if card[0] == suit: 
       counts[suit] += 1 
    return counts