import random , math , time , sys

from random import choice as rc

# Blackjack

def playAgain ( ) :

print ( 'Do you want to play again? (yes or no)' )

return input ( ) . lower ( ) . startswith ( 'y' )

def money ( ) :

money = 10

if cwin == False :

money + = 2 * int ( bet )

if tie == True :

money + = int ( bet )

else :

money - = int ( bet )

return money

def outOfMoney ( ) :

if money <= 0 :

print ( 'You have run out of money! Game over!' )

return True

else :

return False

return False

def getCard ( cardList ) :

cardIndex = random . randint ( 0 , len ( cardList ) - 1 )

return cardList [ cardIndex ]

def sumHand ( hand ) :

total = 0

for card in hand:

if card == 11 :

total + = 11

else :

total + = int ( card )

while total > 21 and 11 in hand: #If the total is greater than 21, set 11 equal to 1

total - = 10

while total > 21 and 11 in hand:

total - = 10

while total > 21 and 11 in hand:

total - = 10

while total > 21 and 11 in hand:

total - = 10

return total

money = 10

print ( 'Welcome to BlackJack! Press Enter to play.' )

input ( )

def play ( money ) :

cards = [ 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 10 , 10 , 10 , 11 ] #Card List

if money <= 0 :

outOfMoney ( )

print ( 'You have %s dollars. What would you like to bet?' % ( money ) )

random . shuffle ( cards )

player = [ ]

player. append ( getCard ( cards ) ) #The command to get a card for a player

player. append ( getCard ( cards ) )

comp = [ ]

comp. append ( getCard ( cards ) )

gameisDone = False

bet = input ( )

money - = int ( bet )

while True :

if sumHand ( player ) < 22 :

tp = sumHand ( player )

tc = sumHand ( comp )

print ( 'Your cards are: %s with a total value of %d' % ( player , sumHand ( player ) ) )

print ( 'The dealers visible card is %s' % ( comp ) )

print ( 'Hit or Stand?' )

choice = input ( )

if choice. upper ( ) == 'HIT' :

player. append ( getCard ( cards ) )

else :

break

else :

break

cbust = False

pbust = False

comp. append ( getCard ( cards ) )

tp = sumHand ( player )

tc = sumHand ( comp )

print ( 'Your final cards are %s value is %d' % ( player , tp ) )

time . sleep ( 1 )

print ( 'The dealers cards are %s with a value of %r' % ( comp , tc ) )

time . sleep ( 1 )

if sumHand ( player ) > 21 :

pbust = True

print ( 'You have busted! The dealer has won!' )

time . sleep ( 1 )

gameisDone = True

cwin = True

if pbust == False :

if sumHand ( comp ) < 16 :

comp. append ( getCard ( cards ) )

print ( 'The dealers cards are %s with a value of %r' % ( comp , sumHand ( comp ) ) )

time . sleep ( 1 )

if sumHand ( comp ) < 16 :

tc = sumHand ( comp )

comp. append ( getCard ( cards ) )

print ( 'The dealers cards are %s with a value of %r' % ( comp , sumHand ( comp ) ) )

time . sleep ( 1 )

print ( 'The dealers final cards are %s with a value of %r' % ( comp , sumHand ( comp ) ) )

if 22 > sumHand ( comp ) > sumHand ( player ) :

time . sleep ( 1 )

print ( 'The dealer has won!' )

cwin = True

gameisDone = True

if 22 > sumHand ( player ) > sumHand ( comp ) :

time . sleep ( 1 )

print ( 'Congradulations! You have won!' )

money + = 2 * int ( bet )

cwin = False

gameisDone = True

if sumHand ( comp ) > 21 :

time . sleep ( 1 )

print ( 'The dealer has busted. You have won!' )

money + = 2 * int ( bet )

cwin = False

gameisDone = True

if sumHand ( comp ) == sumHand ( player ) :

time . sleep ( 1 )

print ( 'The game has tied!' )

money + = int ( bet )

gameisDone = True

tie = True

if gameisDone:

if outOfMoney ( ) == False :

if playAgain ( ) :

play ( money )

play ( money )