Files
stuff/activity3-transfer.py

78 lines
1.9 KiB
Python
Raw Normal View History

2026-02-20 11:42:22 +11:00
# Created by Maxwell Jeffress on 17/02/2026
# Simulates a game of Blackjack
import random
2026-02-20 11:59:02 +11:00
# Generate cards
# The first list is for cards 2-10, the second is for cards J-A
cards = [card for card in range(2, 11) for _ in range(4)] + [10 for _ in range(0, 16)]
2026-02-20 11:42:22 +11:00
def card_string(card: int) -> str:
match card:
case 11:
return "Jack"
case 12:
return "Queen"
case 13:
return "King"
case 14:
return "Ace"
case _:
return str(card)
# Create the card order
random.shuffle(cards)
def deal_card(cards: [int]) -> int:
return cards.pop()
you = 0
dealer = 0
card1 = deal_card(cards)
card2 = deal_card(cards)
you += card1 + card2
print("You got dealt", card_string(card1), "and", card_string(card2), "for a total of", you)
dealer += deal_card(cards) + deal_card(cards)
dealerWon = False
while True:
hors = input("Hit or stand? ")
if hors.lower() in ["h", "hi", "hit", "hit me"]:
print("Hitting")
card = deal_card(cards)
you += card
print("You got dealt", card_string(card), "for a total of", you)
if (you > 21):
print("Bust!")
dealerWon = True
break
elif hors.lower() in ["s", "st", "sta", "stan", "stand", "stand up"]:
print("Standing")
break
else:
print("That's not valid!")
continue
if not dealerWon:
print("Dealing the dealer")
2026-02-20 11:59:02 +11:00
print("Dealer has", dealer)
while dealer < 16:
card = deal_card(cards)
dealer += card
print("Dealer got", card_string(card), "they have", dealer)
if dealer > 21:
print("Dealer is bust!")
dealerWon = False
break
if dealerWon:
if you > dealer:
print("You win!")
else:
print("Dealer wins...")