0

我是 python 新手,我正在尝试编写一个代码来处理扑克牌并检查拍拍冲洗。下面是我尝试运行它时的代码和外壳。根据我的教授的说法,True如果手中只有一套西装,即只有一套“西装”中的一个条目,False否则这应该返回,但我不断收到此错误消息。有人可以帮我解释一下吗?

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

>>> 
4

1 回答 1

0

你在打电话is_flush(5)。如果我对您的理解正确,那么该值 5 是hand您尝试迭代的变量(就好像它是一只手一样) in c[-1] for c in hand,但您不能迭代整数。我感到困惑的原因是我希望它说它是一个 IntType,而不是一个 NoneType。

于 2012-09-20T18:55:46.987 回答