day 11
import random
def deal_cards():
cards = [11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10]
card = random.choice(cards)
return card
user_cards = []
computer_cards = []
for _ in range(2):
user_cards.append(deal_cards())
computer_cards.append(deal_cards())
print(user_cards)
print(computer_cards)
def calculate_scores(cards: list):
if sum(cards) == 21 and len(cards) == 2:
return 0
if sum(cards) > 21 and 11 in cards:
cards.remove(11)
cards.append(1)
return sum(cards)
def compare(user_score, computer_score):
if user_score == computer_score:
return "Draw"
elif computer_score == 0:
return "Lose, oppoent has Blackjack"
elif user_score == 0:
return "Win the Blackjack"
elif user_score > 21:
return "You went over"
elif computer_score > 21:
return "Oppoent went over. You win"
elif user_score > computer_score:
return "You win"
else:
return "You lose"
is_game_over = False
while not is_game_over:
user_score = calculate_scores(user_cards)
computer_score = calculate_scores(computer_cards)
print(f' Your cards:{user_cards}, current score: {user_score}')
print("Computer's first card:{}".format(computer_cards[0]))
if user_score == 0 or computer_score == 0 or user_score > 21:
is_game_over = True
else:
user_should_deal = input("Type 'y' to get another card or enter other to pass: ")
if user_should_deal == 'y':
user_cards.append(deal_cards())
else:
is_game_over = True
while computer_score and computer_score < 17:
computer_cards.append(deal_cards())
computer_score = calculate_scores(computer_cards)
print('Your final hand is: ' , user_cards)
print("Computer's final hands is ", computer_cards)
print(compare(user_score, computer_score))