Making a Blackjack game
- 라임 샹큼
- Feb 28
- 2 min read
Blackjack is a card game where you are given 2 cards and either hold or choose new cards depending on whether you feel you are closer to 21. If your held cards are bigger than the opposing side's cards, you win.
import random
cards = [11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10]
card ={
'computer': [],
'you':[]
}
def pick():
card['you'].append(random.choice(cards))
card['computer'].append(random.choice(cards))
if sum(card['you']) > 21:
print(f'BUST.your cards are {card["you"]}. your cards are above 21')
choice3 = input('type y to restart game, type n to exit ').lower()
return 'bust'
else:
print(f'your cards are {card["you"]}')
print(f'computers cards are {card["computer"][0:len(card["computer"])-1]}')
def hold():
print(f'computers cards were {card["computer"]}')
if sum(card['computer']) < sum(card['you']) <= 21:
print('You won!!')
elif sum(card['computer']) == sum(card['you']):
print('It\'s a tie!')
elif sum(card['you']) < sum(card['computer']) <= 21:
print('You lost...')
elif sum(card['computer']) > 21:
print('Computer overrode 21, you won!')
choice3 = input('type y to restart game, type n to exit ').lower()
if choice3 == 'y':
card['computer'] = []
card['you'] = []
black()
else:
exit()
def black():
print()
choice3 = 'y'
while choice3 == 'y':
for e in range(2):
for element in card:
card[element].append(random.choice(cards))
print(f'your cards are {card["you"]}')
print(f'computers 1st card is {card["computer"][0]}')
choice = input('Will you hold or pick?').lower()
if choice == 'hold':
hold()
elif choice == 'pick':
while choice == 'pick':
if pick() == 'bust':
break
else:
choice = input('Will you hold or pick?').lower()
if len(card['you']) < 21:
hold()
choice3 = input('type y to restart game, type n to exit ').lower()
black()
Comments