需要帮助了解为什么我的黑杰克游戏出现“IndexError:从空列表中弹出”错误

问题描述 投票:0回答:3

我试图制作一个二十一点游戏,当我输入 H 或 D 作为选择输入时,除了 Ctrl+C 之外,控制台不允许我进行任何输入 output1 并且它会产生 ouput2 中显示的错误当我输入 S 作为选择输入时

我的代码

from random import choice, shuffle
plain_cards = ['2','3','4','5','6','7','8','9','J', 'K', 'Q', 'A']
HEARTS = chr(9829)
DIAMONDS = chr(9830)
SPADES = chr(9824)
CLUBS = chr(9827)

suits = [HEARTS,DIAMONDS,SPADES,CLUBS]
def get_deck():
    deck = [(suit, card) for suit in suits for card in plain_cards]
    shuffle(deck)
    return deck

def get_cards_value(cards):
    total = 0
    aces = []
    for card in cards:
        if card[1].isdigit():
            total += int(card[1])
        if card[1] == 'K' or card[1] == 'J' or card[1] == 'Q':
            total += 10
        if card[1] == 'A' :
            total += 11
            aces.append('A')
    for i in aces:
        if total > 21:
            total -= 10

    return total


def game():
    print('Welcome to BLACK JACK')

money = 1000
playing = True
game_on = True
while playing:
    if money <= 0:
        playing = False    
        print('Thanks for playing but your money is done come back soon')
        
    else:
        bet = input('Stake: ')
        
        if  not bet.isdigit():
            print('Invalid input')
            continue
        elif int(bet) > money:
            print('You don\'t have enough money') 
            continue
        else:
            bet = int(bet)
            deck = get_deck()
            player_hand = [deck.pop(), deck.pop()]
            dealer_hand = [deck.pop(), deck.pop()]
            print('Player hand')
            print(f'{player_hand} total:{get_cards_value(player_hand)}')
            print('Dealer Hand')
            print(f'[{dealer_hand[0]}, ###]')
            
            player_total = get_cards_value(player_hand)
            dealer_total = get_cards_value(dealer_hand)
            while game_on:
                if player_total > 21 or dealer_total > 21:
                    break
                choice = input('(S)tand, (D)ouble, (H)it: ')
                if choice == 'D':
                    bet *= 2
                    money += bet 
                    
                if choice == 'D' or choice == 'H':
                    new_card = deck.pop()
                    player_hand.append(new_card)
                    break
                
                if choice == 'S':
                    break
                
            if player_total < 21:
                while dealer_total < 17:
                    new_card = deck.pop()
                    dealer_hand.append(new_card)
            
            if player_total == 21:
                print('You won')
                print(f'Player>>{player_hand} total:{get_cards_value(player_hand)}')
                print(f'Dealer>>{dealer_hand} total:{dealer_total}')
                money += bet
            
            elif dealer_total == 21:
                print('You lost')
                print(f'Player>>{player_hand} total:{get_cards_value(player_hand)}')
                print(f'Dealer>>{dealer_hand} total:{dealer_total}')
                money -= bet
                
            elif player_total > dealer_total:
                print('You lost')
                print(f'Player>>{player_hand} total:{get_cards_value(player_hand)}')
                print(f'Dealer>>{dealer_hand} total:{dealer_total}')
                money -= bet
                
            elif player_total < dealer_total:
                print('You win')
                print(f'Player>>{player_hand} total:{get_cards_value(player_hand)}')
                print(f'Dealer>>{dealer_hand} total:{dealer_total}')
                money += bet
            
game()

这是我输入 S、H 或 D 时的输出 回溯(最近一次调用最后一次):

File "C:/Users/Godfrey Baguma/AppData/Local/Programs/Python/Python39/ssdd.py", line 109, in <module>
    game()
  File "C:/Users/Godfrey Baguma/AppData/Local/Programs/Python/Python39/ssdd.py", line 82, in game
    new_card = deck.pop()
IndexError: pop from empty list
 
python blackjack
3个回答
2
投票

该错误表明您想要访问一个未知的变量。您的代码中有一个错误(第 82 或 83 行)。程序流程可以通过调用

dealer_hand.append(new_card)
但尚不知道
new_card
来进行。


2
投票

答案就在错误输出中。尝试更深入地观察。


1
投票

存在无限循环:

while dealer_total < 17:
    new_card = deck.pop()
    dealer_hand.append(new_card)

dealer_total
未更新,因此
deck
用完卡片并抛出
IndexError

© www.soinside.com 2019 - 2024. All rights reserved.