I have just started learning python and was trying to write a very simple version of Black Jack game. The code seems to be running fine except sometimes it will simply stop after printing "Now dealer moves" with no error message or reason.
from random import choices, choice
import inquirer
new_game = True
while new_game:
cards = [11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10]
player_hand = choices(cards,k=2)
dealer_hand = choices(cards,k=2)
player_sum = sum(player_hand)
dealer_sum = sum(dealer_hand)
print(player_hand)
print(dealer_hand)
print(f"Your cards are {player_hand} with a score of {player_sum}.")
print(f"The dealer's first card is {dealer_hand[0]}.")
print("\nNow you move first.")
player_end = False
while not player_end:
another_round = input("Type 'y' to deal another card, type 'n' to pass: ").lower()
if another_round == 'y':
player_hand.append(choice(cards))
player_sum = sum(player_hand)
if player_sum > 21 or another_round == 'n':
player_end = True
print(f"Your final hand is {player_hand} with a score of {player_sum}.")
else:
print(f"Your hand is now {player_hand} and current score is {player_sum}.")
if player_sum > 21:
print("Unfortunately you went over. You lose")
else:
print('\nNow dealer moves.')
dealer_end = False
while not dealer_end:
if dealer_sum < 17:
dealer_hand.append(choice(cards))
dealer_sum = sum(player_hand)
else:
dealer_end = True
print(f"The dealer's final hand is {dealer_hand} with a score of {dealer_sum}.")
if player_sum > dealer_sum:
print("You won!")
elif player_sum == dealer_sum:
print("It's a draw")
else:
print("You lose.")
another_game = input("\nDo you like to start a new game? Type 'y' to start and 'n' to exit. ")
if another_game == 'n':
new_game = False
else:
print("\n")
I have tried running it multple time, but still don't see a clear pattern of when it would stop working.
Can anyone help me to spot the error?
Aucun commentaire:
Enregistrer un commentaire