需要有关为什么我的二十一点游戏的帮助 收到"索引错误:从空列表中弹出"错误



我试图制作一个21点游戏,当我输入H或D作为选择输入时,控制台不允许我输入任何输入,除了Ctrl+Coutput1当我输入S作为选择输入

时,它会产生如output2所示的错误

我的代码
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

答案在这里的错误输出中。具体来说,在这个块中:

if player_total < 21:
while dealer_total < 17:
new_card
dealer_hand.append(new_card)

你没有设置new_card的值(你是想让它变成new_card = deck.pop()吗?),所以解释器不知道该怎么处理它,然后报错。

错误提示您要访问一个未知的变量。代码中有一个错误(第82或83行)。程序流可以这样发生:dealer_hand.append(new_card)被调用,但new_card还不知道。

有一个无限循环:

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

dealer_total没有更新,所以deck用完卡并抛出IndexError

最新更新