问题描述
我正在尝试制作二十一点游戏,当我为选择输入输入 H 或 D 时,除了 output1 和 Ctrl+C 之外,控制台不允许我的任何输入当我为选择输入输入 S 时,它会产生一个显示在 ouput2 中的错误
我的代码
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
未知的方式发生。