Python开发21点游戏出现IndexError: pop from empty list错误求助
报错与无响应根因
- 输入S后触发
IndexError: pop from empty list的核心原因:庄家补牌逻辑未更新dealer_total变量。你初始化dealer_total为庄家两张初始手牌的点数后,后续给庄家补牌时从未重新计算点数,导致初始庄家点数小于17时,while dealer_total < 17会进入死循环,持续从牌堆pop牌直到牌堆为空抛出报错。 - 输入H/D后控制台无响应的原因:你将游戏主逻辑错误写在了
game()函数外部,且全局变量game_on仅初始化一次为True,同时玩家补牌后未更新player_total变量,触发逻辑死循环。 - 隐藏问题:你导入了
from random import choice,后续用choice = input(...)覆盖了随机库的choice方法,会导致后续用到随机选择的逻辑出错;Double下注逻辑错误,未结算就直接给账户加钱;胜负判断时使用的是初始的玩家/庄家点数,结果全部错误。
可运行修复后代码
from random import 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 = 0 for card in cards: if card[1].isdigit(): total += int(card[1]) elif card[1] in ('K','J','Q'): total += 10 elif card[1] == 'A' : total += 11 aces += 1 while total > 21 and aces > 0: total -= 10 aces -= 1 return total def game(): print('Welcome to BLACK JACK') money = 1000 playing = True while playing: if money <= 0: playing = False print('Thanks for playing but your money is done come back soon') continue bet = input('Stake: ') if not bet.isdigit(): print('Invalid input') continue bet = int(bet) if bet > money: print('You don\'t have enough money') continue deck = get_deck() player_hand = [deck.pop(), deck.pop()] dealer_hand = [deck.pop(), deck.pop()] print('Player hand') player_total = get_cards_value(player_hand) print(f'{player_hand} total:{player_total}') print('Dealer Hand') print(f'[{dealer_hand[0]}, ###]') dealer_total = get_cards_value(dealer_hand) game_on = True while game_on: player_total = get_cards_value(player_hand) if player_total > 21 or dealer_total > 21: break user_choice = input('(S)tand, (D)ouble, (H)it: ').upper() if user_choice == 'D': bet *= 2 new_card = deck.pop() player_hand.append(new_card) player_total = get_cards_value(player_hand) break elif user_choice == 'H': new_card = deck.pop() player_hand.append(new_card) player_total = get_cards_value(player_hand) if player_total > 21: break elif user_choice == 'S': break if player_total <= 21: while get_cards_value(dealer_hand) < 17: new_card = deck.pop() dealer_hand.append(new_card) dealer_total = get_cards_value(dealer_hand) print(f'Player>>{player_hand} total:{player_total}') print(f'Dealer>>{dealer_hand} total:{dealer_total}') if player_total > 21: print('You bust, you lost') money -= bet elif dealer_total > 21: print('Dealer bust, you win') money += bet elif player_total == 21 and len(player_hand) == 2: print('Blackjack! You won') money += int(bet * 1.5) elif player_total == dealer_total: print('Push, no win no loss') elif player_total > dealer_total: print('You win') money += bet else: print('You lost') money -= bet print(f'Your current balance: {money}') if __name__ == '__main__': game()
内容的提问来源于stack exchange,提问作者ALFRED MARVIN
相关产品推荐
相关产品推荐

