Professional Documents
Culture Documents
In [ ]: import random
playing = True
#hand class to add cards to empty list and caluculate value of hand
class Hand:
def __init__(self, dealer_hand=False):
self.deck = []
self.value = 0
self.aces = 0
def add_card(self,card):
self.deck.append(card)
self.value += num_vals[card.name]
def get_value(self):
if player_hand.deck(name_vals) == 'Ace':
self.aces += 1
while self.value > 21 and self.aces:
self.value -= 10
self.aces -= 1
return self.value
localhost:8888/nbconvert/html/Code/Blackjack.ipynb?download=false 2/4
12/8/2020 Blackjack
player_chips = 10
#while playing:, add 'while true' fucntion here to start new round until quit
while playing:
print('Welcome to a new game of Blackjack!')
deck = Deck()
deck.shuffle_deck()
while True:
try:
print('You have', player_chips, 'chips.', end=' ')
bet_amount = int(input('How many would you like to bet? \n'))
if bet_amount <= 0:
print('Sorry, please enter a postitive integer!')
elif bet_amount > player_chips:
print('Sorry, you do not have enough chips')
elif bet_amount in range(0,player_chips):
player_chips -= bet_amount
print('\nYou bet', bet_amount,'chips. You have', player_chips,
'chips left.')
break
except ValueError:
print('Sorry, please enter an integer!')
player_hand = Hand()
player_hand.add_card(deck.deal_deck())
player_hand.add_card(deck.deal_deck())
dealer_hand = Hand()
dealer_hand.add_card(deck.deal_deck())
dealer_hand.add_card(deck.deal_deck())
localhost:8888/nbconvert/html/Code/Blackjack.ipynb?download=false 3/4
12/8/2020 Blackjack
player_score = player_hand.get_value()
dealer_score = dealer_hand.get_value()
while True:
try:
restart = input('Would you like to play another game? Yes or no?\n
')
if restart in ['No', 'no', 'n']:
playing = False
break
elif restart in ['Yes', 'yes', 'y']:
break
continue
except ValueError:
print('Sorry, that is not a valid input!')
localhost:8888/nbconvert/html/Code/Blackjack.ipynb?download=false 4/4