我不明白为什么这会给我一个错误

时间:2016-03-25 00:24:15

标签: python list nonetype

我正在尝试创建最终模拟二十一点的程序的一部分。

def getShuffleDeck():
    newDeck = ['A','2','3','4','5','6','7','8','9','T','J','Q','K'] * 4
    shuffleDeck = []
    print(newDeck)
    deckPreshuffle = list(newDeck)
    shuffleDeck = random.shuffle(newDeck)
    return shuffleDeck


def dealCard(playingDeck):
    playDeck = []
    player1CardDealt = playDeck[0]
    print("Dealer gave you: " + playDeck[0])
    player2CardDealt = playDeck[1]
    print("Dealer is dealt a card.")

    playingDeck.remove(playerCardDealt)
    playingDeck.remove(dealerCardDealt)
    return (playerCardDealt, dealerCardDealt, playDeck)

# def cardValues(cardDealt):
dealCards = []
dealCards = (getShuffleDeck())
print(dealCards)


# print(dealtCard)

player1Card = dealCards
player2Card = dealCards[1]
currentDeck = dealCards[2]
print(player1Card)
print(player2Card)

该程序给我一个非类型错误。有人可以向我解释我做错了吗?

1 个答案:

答案 0 :(得分:2)

playDeck = []
player1CardDealt = playDeck[0]
由于您尝试访问空列表的第一个元素,

总是会给您一个错误...

此外

def getShuffleDeck():
    newDeck = ['A','2','3','4','5','6','7','8','9','T','J','Q','K'] * 4
    shuffleDeck = []
    print(newDeck)
    deckPreshuffle = list(newDeck)
    shuffleDeck = random.shuffle(newDeck)
    return shuffleDeck

将始终返回None ...因为random.shuffle将牌组洗牌并且没有返回值(我至少可以肯定)