I am new at Python, and I am building a blackjack game as a capstone project. I think the code is doing pretty well besides a random error that I keep randomly getting different times of running the code. Sometimes the code will run perfectly but, sometimes it randomly just crashes when it hits the below code. sometimes it happens in the beginning and sometimes it may happen when it is going through the while loop again. Sometimes it goes through the same process and never has an issue.
Could someone explain why I keep getting this error? I understand what the error means but, non- of the codes are out of range?? or is it??
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
I know the functions are messy. I have not learned how to use classes yet and so I don't know how to be tidy yet. but I did remove function parameters in initial_cards() to avoid passing empty list for now... but, I doubt that was the issue. I also change my indents from tabs to space... not sure if that is an issue.