-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblackjack.py
More file actions
101 lines (93 loc) · 2.85 KB
/
blackjack.py
File metadata and controls
101 lines (93 loc) · 2.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import random
import os
a=True
user_score=0
while a==True:
def calc_hand(hand):
sum=0
non_aces=[card for card in hand if card!='A' ]
aces=[card for card in hand if card=='A' ]
for card in non_aces:
if card in 'JKQ':
sum+=10
else:
sum+=int(card)
for card in aces:
if sum<=10:
sum+=11
else:
sum+=1
return sum
cards=[
'2','3','4','5','6','7','8','9','10','J','K','Q','A',
'2','3','4','5','6','7','8','9','10','J','K','Q','A',
'2','3','4','5','6','7','8','9','10','J','K','Q','A',
'2','3','4','5','6','7','8','9','10','J','K','Q','A'
]
random.shuffle(cards)
dealer=[]
player=[]
player.append(cards.pop())
dealer.append(cards.pop())
player.append(cards.pop())
dealer.append(cards.pop())
standing=False
firsthand=True
# print(dealer,player)
while True:
os.system('cls' if os.name=='nt' else 'clear')
player_score=calc_hand(player)
dealer_score=calc_hand(dealer)
if standing:
print('Dealer cards [{}] ({})'.format(']['.join(dealer),dealer_score))
else:
print(f'Dealer cards :[{dealer[0]}][?]')
print('ur cards [{}] ({})'.format(']['.join(player),player_score))
if standing:
if dealer_score>21:
print('Dealer busted .. U win!!')
user_score+=100
print("user score:" ,user_score)
elif player_score==dealer_score:
print('Push, nobody wins or loses')
user_score+=0
print("user score:" ,user_score)
elif player_score>dealer_score:
print('U beat the dealer, u win!')
user_score+=100
print("user score:" ,user_score)
else:
print('u lose:(')
user_score-=100
print("user score:" ,user_score)
break
if player_score>21:
print(' u r busted!!')
user_score-=100
print("user score:" ,user_score)
break
firsthand=False
if firsthand and player_score==21:
print('BlackJack ! U win ')
user_score+=100
print("user score:" ,user_score)
break
print('what would u like to do?')
print('[1] Hit')
print('[2] Stand')
print('')
print('Your choice')
choice=input('Your choice: ')
print('')
if choice=='1':
player.append(cards.pop())
elif choice=='2':
standing=True
while calc_hand(dealer)<=16:
dealer.append(cards.pop())
print("to continue:press y")
c=input()
if c=='y':
a=True
else:
a=False