我是新來的蟒蛇,我正在努力編寫一個代碼來處理撲克牌手並檢查拍子沖水。當我嘗試運行它時,下面是我的代碼和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
>>>
您可能需要在該行之前調試print'hand',以防萬一。我懷疑這是一個副作用問題。 – nemesisfixx