下面是我寫的一些Python代碼,用於生成一個隨機的撲克牌手,只是爲了它/爲了挑戰,但是當我嘗試運行它時,我得到上面的錯誤「if card at hand」 。發生了什麼事情,爲什麼會發生這種情況,尤其是因爲該線路沒有迭代?「TypeError:類型'NoneType'的參數不可迭代」?
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
... doh。我知道。感謝您提醒我使用列表的基本知識之一。 ......仍然不知道我是如何忘記的。再次感謝。 – IronBeard