Python黑杰克游戏随机出现IndexError: list index out of range问题求助
黑杰克游戏随机触发IndexError问题排查
我是Python新手,正在开发黑杰克游戏作为结业项目。代码多数时候运行正常,但会随机崩溃,触发IndexError: list index out of range错误,该错误有时在初始化阶段出现,有时在循环过程中触发。我理解该错误含义,但无法定位代码中索引越界的原因。
报错栈信息
Traceback (most recent call last):
File "C:\Users\carlo\Documents\training\confusion_list_call.py", line 49, in
initial_card()
File "C:\Users\carlo\Documents\training\confusion_list_call.py", line 24, in initial_card
computer_card.append(cards[random.randint(0,len(cards))])
IndexError: list index out of range
代码实现
import random cards = [11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10] computer_card = [] user_card = [] computer_total = 0 user_total = 0 def computer_deal(): global computer_card computer_card.append(cards[random.randint(0,len(cards))]) def user_deal(): global user_card user_card.append(cards[random.randint(0,len(cards))]) def initial_card(): for _initial in range(1, 3): global user_card global computer_card user_card.append(cards[random.randint(0,len(cards))]) ##randomly happens here or next line computer_card.append(cards[random.randint(0,len(cards))]) def sum_cards(): global computer_total global user_total user_total = 0 computer_total = 0 for total_c in computer_card: computer_total += total_c for total_u in user_card: user_total += total_u def reset_game(): global computer_total global user_total global computer_card global user_card computer_total = 0 user_total = 0 computer_card.clear() user_card.clear() continue_game = True while continue_game: initial_card() print(f"Your cards: {user_card} \nComputer's Card {computer_card[0]}") choice = input("Would you like another card 'y' or 'n' \n ") no_bust = True if choice == 'y': while no_bust: user_deal() sum_cards() print(f"Users new deck is: {user_card}") print(f"Computer's total: {computer_total} and your total: {user_total}\n ") if user_total > 21: print("You loss!") no_bust = False elif computer_total == user_total: print("push: tied! ") no_bust = False else: choice = input("Would you like another card 'n' or 'y'") if choice == 'y': no_bust = True if choice == 'n': if computer_total == user_total: print("push: tied! ") no_bust = False if choice == 'n': if computer_total >= 17 and computer_total < 21: if computer_total > user_total: print(f"You loss!, Computer's total: {computer_total} and your total: {user_total}") else: print(f"You Win!, Computer's total: {computer_total} and your total: {user_total}") under_17 = True while under_17: if computer_total < 17: computer_deal() sum_cards() print(f"test3: computers new deck ~~~ > {computer_card}\nTest4: computers new total ~~~~ > {computer_total}") if computer_total == user_total: print("Game is tied: push") under_17 = False elif computer_total > 21: print("Computer has bust, You won!") under_17 = False elif computer_total >= 17 and computer_total <= 21: if computer_total > user_total: print(f"test5: Computer's total: {computer_total} and your total: {user_total}") print("You lose!") under_17 = False if computer_total < user_total: print("You win!") under_17 = False else: under_17 = True continue_choice = input("Would you like to play another game, 'y' or 'n'") if continue_choice == 'n': print("Thank you for playing Black Jack, have a good day!") continue_game = False if continue_choice == 'y': reset_game() continue_game is True
我知道代码结构较混乱,因为还没学习类的使用。我曾移除initial_card()的函数参数,也将缩进从制表符改为空格,但问题仍未解决。
内容的提问来源于stack exchange,提问作者Carlos Colón
相关产品推荐
相关产品推荐

