Im complete beginner in my Python journey and im learning through videos and couple of courses, ive reached my first capstone project – simple Blackjack and ive decided to power through and not use any help with the structure of my code and this is the result ive end up with, but it seems there is exceptions in my code which i struggle to see, the image shows that code enters some sort of loop, apologies for the sloppy code :(enter image description here
import random
cards = [11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10]
game_start = input("Do you want to play a game of Blackjack? Type 'y' or 'n': ")
player_cards = []
player_score = 0
computer_cards = []
computer_score = 0
ace_count = 0
def ace():
if 11 in player_cards:
global ace_count
global player_score
ace_count += 1
if player_score > 21 and ace_count != 0:
player_score -= 10
def player_deal():
deal = random.choice(cards)
player_cards.append(deal)
global player_score
player_score += deal
ace()
return
def computer_deal():
deal = random.choice(cards)
computer_cards.append(deal)
global computer_score
computer_score += deal
ace()
return
def score():
print(f"Player cards are [{player_cards}], and Player score is {player_score}")
print(f"Computer cards are [{computer_cards}], and Computer score is [{computer_score}]")
if game_start == 'y':
player_deal()
player_deal()
ace()
print(f"Player cards are [{player_cards}], and Player score is {player_score}")
computer_deal()
computer_deal()
ace()
print(f"Computer first card is [{computer_cards[0]}]")
if computer_score == 21:
score()
print("Computer wins!")
elif player_score == 21:
score()
print("Player wins!")
else:
while player_score < 21 or computer_score < 21:
another_card = input("Pres 'y' to get another card, Press 'n' to pass: ")
if another_card == 'y':
player_deal()
if player_score == 21:
score()
print("Player Wins!")
break
elif player_score > 21:
score()
print("Computer Wins!")
break
else:
if computer_score < 17:
computer_deal()
if computer_score > 21:
score()
print("Player wins!")
break
elif player_score > computer_score:
score()
print("Player wins!")
break
elif player_score < computer_score:
score()
print("Computer wins!")
break
else:
score()
print("It's a draw!")
break
else:
print("Have a nice day!")
After pressing ‘y’ to continue and draw more cards, my while loop seems to enter an infinity loop, sometimes it happens when i press ‘n’ too
Miroslav Georgiev is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.