So I am trying to make a simple blackjack game in python 3 and everything works just fine, except that I can't stand. If I input 2 it won't do anything. Thanks in advance. EDIT 1: Hitting works well. EDIT 2: Posted the entire script, so you can reproduce the problem I'm facing, as @roganjosh suggested.
from random import shuffle
import sys
def deal(deck, player, dealer):
shuffle(deck)
for _ in range(2):
player.append(deck.pop())
dealer.append(deck.pop())
def score(hand):
non_aces = [c for c in hand if c != 'A']
aces = [c for c in hand if c == 'A']
sum = 0
for card in non_aces:
if card in 'JQK':
sum += 10
else:
sum += int(card)
for card in aces:
if sum <= 10:
sum += 11
else:
sum += 1
return sum
def display_info(player, dealer, stand):
print("Your cards: [{}] ({})".format(']['.join(player), score(player)))
if stand:
print("Dealer cards: [{}] ({})".format(']['.join(dealer), score(dealer)))
else:
print("Dealer cards: [{}] [?]".format(dealer[0]))
def results(player, dealer, hand, stand):
if score(player) == 21 and hand:
print("Blackjack! You won!")
sys.exit()
elif score(player) > 21:
print("Busted. You lost!")
sys.exit()
if stand:
if score(dealer) > 21:
print("Dealer busted. You won!")
elif score(player) > score(dealer):
print("You beat the dealer! You won!")
elif score(player) < score(dealer):
print("You lost!")
else:
print("Push. Nobody wins or losses.")
sys.exit()
def hit_stand(deck, player, dealer, hand, stand):
print("What would you like to do")
print("[1] - Hit\n[2] - Stand")
choice = input("> ")
hand = False
if choice == '1':
player.append(deck.pop())
elif choice == '2':
stand = True
while score(dealer) <= 16:
dealer.append(deck.pop())
if __name__ == '__main__':
deck = ['A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K']*4
player = []
dealer = []
standing = False
first_hand = True
deal(deck, player, dealer)
while True:
display_info(player, dealer, standing)
results(player, dealer, first_hand, standing)
hit_stand(deck, player, dealer, first_hand, standing)