-
Notifications
You must be signed in to change notification settings - Fork 0
/
9.9.py
129 lines (121 loc) · 2.99 KB
/
9.9.py
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
from random import choice
#import pdb
D = tuple(range(1,11))
def main():
ans = printIntro()
while ans in {"y", "Y", "yes", "Yes", "YES"}:
result = dealDealerHand(ans)
printEnd(result)
ans = printIntro()
def printIntro():
ans = input("Play a game of blackjack (y/n)? ")
return ans
def dealDealerHand(ans):
dealerHand = []
userHand = []
if ans in {"y", "Y", "yes", "Yes", "YES"}:
while sum(dealerHand) < 17:
nextCard = choice(D)
dealerHand.append(nextCard)
print("{} %".format(100 * sum(i > (21 - sum(dealerHand)) for i in D)/10))
print(dealerHand[0])
print(sum(dealerHand))
resultD = blackjackD(dealerHand)
resultU = blackjackU(userHand)
result = [resultD, resultU]
else:
result = "uninterested"
return result
def blackjackD(dealerHand):
if 1 in dealerHand:
hasAce = True
else:
hasAce = False
if (hasAce and sum(dealerHand) == 11):
print(dealerHand)
print(sum(dealerHand))
result = "Dealer Wins!"
elif hasAce and 6 <= sum(dealerHand) <= 11:
dealerHand[dealerHand.index(1)] = 11
elif sum(dealerHand) > 21:
print(dealerHand)
print(sum(dealerHand))
result = "User Wins!"
else:
result = dealerHand
return result
def blackjackU(userHand):
for i in range(2):
nextCard = choice(D)
userHand.append(nextCard)
print(userHand)
if 1 in userHand:
hasAce = True
else:
hasAce = False
print(sum(userHand))
if hasAce:
print("Has Ace")
if (hasAce and sum(userHand) == 11):
print(userHand)
print(sum(userHand))
result = "User Wins!"
elif hasAce and 6 <= sum(userHand) <= 11:
userHand[userHand.index(1)] = 11
print(userHand)
print(sum(userHand))
ans = input("Hit? ")
while ans in {"y", "Y", "yes", "Yes", "YES"}:
nextCard = choice(D)
userHand.append(nextCard)
print(userHand)
print(sum(userHand))
if 1 in userHand:
hasAce = True
if hasAce:
print("Has Ace")
if (hasAce and sum(userHand) == 11):
print(userHand)
print(sum(userHand))
result = "User Wins!"
break
elif hasAce and 6 <= sum(userHand) <= 11:
userHand[userHand.index(1)] = 11
print(userHand)
print(sum(userHand))
ans = input("Hit? ")
if sum(userHand) > 21:
print(userHand)
print(sum(userHand))
result = "Dealer Wins!"
else:
result = userHand
return result
def printEnd(result):
if result == "uninterested":
end = print("uninterested")
elif result[0] == result[1]:
end = print(result[0])
elif type(result[0]) == str and type(result[1]) != str:
end = print(result[0])
elif type(result[1]) == str and type(result[0]) != str:
end = print(result[1])
elif type(result[0]) == str and result[0] != result[1]:
end = print("No Winner!")
else:
D = sum(result[0])
U = sum(result[1])
print("Dealer had {} which adds up to {}".format(result[0], D))
print("User had {} which adds up to {}".format(result[1], U))
if D == U:
end = print("No Winner!")
elif max(D,U) == D:
print(result[0])
print(D)
end = print("Dealer Wins!")
else:
print(result[1])
print(U)
end = print("User Wins!")
return end
if __name__ == '__main__': main()