Reputation: 39
I was trying to make a blackjack game and when I When I enter H or D for the choice input, the console doesn't allow any of my inputs apart from Ctrl+C for output1 and it produces an error shown in ouput2 when I enter S for the choice input
My code
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()
This the output when I enter S,H or D Traceback (most recent call last):
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
Upvotes: 0
Views: 86
Reputation: 69
The answer's in the error output there. Specifically, in this block:
if player_total < 21:
while dealer_total < 17:
new_card
dealer_hand.append(new_card)
You didn't set the value of new_card (did you mean to make it new_card = deck.pop()?), so the interpreter has no idea what to do with it and errors out.
Upvotes: 2
Reputation: 86
There is an infinite loop:
while dealer_total < 17:
new_card = deck.pop()
dealer_hand.append(new_card)
dealer_total
is not updated, so the deck
runs out of cards and throws the IndexError
.
Upvotes: 1
Reputation: 898
The error says that you want to access a variable that is not yet known. There is a bug in your code (line 82 or 83). The program flow can take place in such a way that dealer_hand.append(new_card)
is called but new_card
is not yet known.
Upvotes: 2