2012-09-20 59 views
0

我是新來的蟒蛇,我正在努力編寫一個代碼來處理撲克牌手並檢查拍子沖水。當我嘗試運行它時,下面是我的代碼和shell。根據我的教授的說法,如果手中只有一件套裝,即套裝中只有一個套裝「適合」,並且False,那麼應該返回True,否則我會收到此錯誤消息。有人可以幫我解釋一下嗎?Python:TypeError:'NoneType'對象不可迭代混淆

from random import * 

suits = {'H','C','D','S'} #hearts, clubs, diamonds, spades 
ranks = {'a','2','3','4','5','6','7','8','9','10','j','q','k'} #card values 
deck = [r +s for r in ranks for s in suits] 
hand = [] 

def deal (n): 
    '''deals n hands''' 

    for n in range (0,n): 
     hand = sample (deck,5) 

     for x in hand: 
      deck.remove (x) 

     print (hand) 

def is_flush (hand): 
    '''checks for pat flush hands''' 
    suits = {c[-1] for c in hand} 
    return len(suits) == 1 

RUN 

>>> is_flush (5) 

['10S', 'qD', '8H', '8D', '3S'] 
['5C', 'jC', 'kS', '4C', '2H'] 
['2S', '7C', '7H', '7S', '9S'] 
['8C', '8S', 'aH', '5S', '2D'] 
['9D', '6S', '4D', 'qS', '9H'] 

Traceback (most recent call last): 
    File "<pyshell#17>", line 1, in <module> 
    is_flush (5) 

    File "K:/stalter_3.py", line 19, in is_flush 
    suits = {c[-1] for c in hand} 

TypeError: 'NoneType' object is not iterable 

>>> 
+0

您可能需要在該行之前調試print'hand',以防萬一。我懷疑這是一個副作用問題。 – nemesisfixx

回答

0

您在致電is_flush(5)。如果我正確理解了你,那麼值爲5的變量hand你試圖在c[-1] for c in hand中迭代(就像它是一隻手),但是你不能遍歷一個整數。我感到困惑的原因是我期望它說它是一個IntType,而不是一個NoneType。

+0

我也試過做is_flush(deal(5))來看看我是否需要它來調用交易函數 – user1686936

+0

這樣會更好,但是請注意,'deal'並不會返回手!你需要添加'return hand'作爲'deal'的最後一行,並且像你所說的'is_flush(deal(5))'調用它。 – Harel