Untitled
unknown
plain_text
a year ago
2.3 kB
6
Indexable
import random
cards = [11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10]
my_hand = []
comp_hand = []
my_hand.append(random.choice(cards))
my_hand.append(random.choice(cards))
comp_hand.append(random.choice(cards))
comp_hand.append(random.choice(cards))
print("Let's play BlackJack!")
def calculate_total(hand):
total = sum(hand)
aces = hand.count(11)
while total > 21 and aces:
total -= 10
aces -= 1
return total
def draw_or_not(cards, my_hand, comp_hand):
while True:
my_hand_total = calculate_total(my_hand)
draw_one_more_card = input(f"Now your hand card is {my_hand}.Your total is {my_hand_total}. Do you want to draw one more card? y/n ")
if draw_one_more_card == "n":
if my_hand_total <= 15:
print("You must draw a card")
else:
comp_draw(my_hand_total, comp_hand,my_hand)
return
elif draw_one_more_card == "y":
my_add_card = random.choice(cards)
my_hand.append(my_add_card)
else:
print("Invalid input. Please enter 'y' or 'n'.")
def comp_draw(my_hand_total, comp_hand,my_hand):
while True:
comp_hand_total = calculate_total(comp_hand)
if comp_hand_total <= 15:
comp_add_card = random.choice(cards)
comp_hand.append(comp_add_card)
else:
win_condition(comp_hand_total, my_hand_total, comp_hand, my_hand)
return
def win_condition(comp_hand_total, my_hand_total, comp_hand, my_hand):
if comp_hand_total > 21:
print(f"Computer busted! You win! Your hand: {my_hand}, Computer's hand: {comp_hand}")
elif my_hand_total > 21:
print(f"You busted! Computer wins! Your hand: {my_hand}, Computer's hand: {comp_hand}")
elif comp_hand_total > my_hand_total:
print(f"Computer wins! Your hand: {my_hand}, Computer's hand: {comp_hand}")
elif comp_hand_total < my_hand_total:
print(f"You win! Your hand: {my_hand}, Computer's hand: {comp_hand}")
else:
print(f"Draw! Your hand: {my_hand}, Computer's hand: {comp_hand}")
draw_or_not(cards, my_hand, comp_hand)Editor is loading...
Leave a Comment