Created
December 23, 2019 00:31
-
-
Save ZoeChiri/782eee0ffb4aaa8c31412de3b1a9b433 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
import time | |
suits = ['♣','♦','♥','♠'] | |
cards = ['K', 'Q', 'J', '10', '9', '8', '7', '6', '5', '4', '3', '2', 'A'] | |
values = {'K':10, 'Q':10, 'J':10, '10':10, '9':9, '8':8, '7':7, '6':6, '5':5, '4':4, '3':3, '2':2, 'A':1} | |
def deal(): | |
suit = random.choice(suits) | |
card = random.choice(cards) | |
pick = card + suit | |
return(pick) | |
def get_total(card_set): | |
score = 0 | |
aces = 0 | |
for card in card_set: | |
if card[:-1] == 'A': | |
aces += 1 | |
for card in card_set: | |
score += values[card[:-1]] | |
if aces > 0: | |
if score + 10 <= 21: | |
return(score + 10) | |
else: | |
return(score) | |
else: | |
return(score) | |
bank = 20 | |
while bank > 0: | |
time.sleep(2) | |
print('Bank = ' + str(bank)) | |
print('Play? Enter \'no\' to quit, enter anything else to play') | |
if input() == 'no': | |
break | |
while True: | |
bet = int(input('Place your bet ')) | |
if bet >= bank: | |
print('Your bet cannot exceed what you have...') | |
else: | |
break | |
bank -= bet | |
print('Bank = ' + str(bank)) | |
dealt = [] #list of cards dealt to avoid repeats | |
hand = [] | |
dealer = [] | |
while True: | |
if len(hand) < 2: | |
input('hit return to deal') | |
while True: | |
new_card = deal() | |
if new_card not in dealt: | |
dealt.append(new_card) | |
hand.append(new_card) | |
break | |
print(hand) | |
new_card = deal() | |
if new_card not in dealt: | |
dealt.append(new_card) | |
dealer.append(new_card) | |
continue ################# | |
if get_total(hand) < 21: | |
choice = input('hit or stand?').lower() | |
if choice == 'stand': | |
break | |
if choice == 'hit': | |
new_card = deal() | |
if new_card not in dealt: | |
dealt.append(new_card) | |
hand.append(new_card) | |
print(hand) | |
else: | |
break | |
if get_total(hand) > 21: | |
print('Bust! Dealer wins!') | |
continue | |
else: # dealer plays | |
print(dealer) | |
while get_total(dealer) <= 16: | |
time.sleep(1.5) | |
new_card = deal() | |
if new_card not in dealt: | |
dealt.append(new_card) | |
dealer.append(new_card) | |
print(dealer) | |
time.sleep(1.5) | |
if get_total(dealer) > 21: | |
print('Dealer bust! You win.') | |
bank += 2 * bet | |
else: | |
print('Your score: ' + str(get_total(hand))) | |
print('Dealer\'s score: ' + str(get_total(dealer))) | |
if get_total(hand) > get_total(dealer): | |
print('You win!') | |
bank += 2 * bet | |
else: | |
print('Dealer wins!') | |
if bank == 0: | |
print('You\'re out of cash!!!') | |
elif bank > 99: | |
print('Bank = ' + str(bank)) | |
print('You\'ve made a lot of cash. Be careful on the way home...') | |
else: | |
print('Bank = ' + str(bank)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment