下面是一些Python代码,我编写这些代码是为了生成一个随机的扑克手,只是为了应付这个挑战,但是当我尝试运行它时,我在上面的“if card in hand”行中得到了错误。?

import random
def pokerHand():
    hand = ["This is your hand:"]
    x = 0
    while x < 5:
        cardNum = random.randrange(13) + 1
        if cardNum == 1:
            cardNum = "Ace of "
        elif cardNum == 11:
            cardNum = "Jack of "
        elif cardNum == 13:
            cardNum = "King of "
        elif cardNum == 12:
        cardNum = "Queen of "
        else:
            cardNum = str(cardNum) + " of "
        cardSuit = random.randrange(4)
        if cardSuit == 0:
            cardSuit = "Clubs"
        elif cardSuit == 1:
            cardSuit = "Diamonds"
        elif cardSuit == 3:
            cardSuit = "Hearts"
        elif cardSuit == 2:
            cardSuit = "Spades"
        card = cardNum + cardSuit
        if card in hand: #<the line of error
            pass
        else:
            hand = hand.append(card)
            x = x + 1
    for xx in hand:
        print xx

最佳答案

hand = hand.append(card)
append不返回任何内容。更改为:
hand.append(card)

09-05 01:18