-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathexercise_8.py
82 lines (67 loc) · 1.77 KB
/
exercise_8.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
#write a program that simulates multiple games of blackjack
#estimates the probability that the dealer will bust
#ace is 1 or 11
#must hit if less than 17
#if ace == 11 makes more than 17 less than 21
#otherwise ace is 1
#bool variable hasAce to determine if hand has an ace
#randrange(1, 13)
#if x == 11 or x == 12 or x == 13
# x = 10
from random import randrange
def main():
printIntro()
n = eval(input("How many hands should I simulate? >> "))
busts = simNHands(n)
printSummary(busts, n)
def printIntro():
print("This program is designed to simulate (n) games of blackjack")
print('and will return the probability of the dealer "busting" or')
print("going beyond 21.")
print()
def simNHands(n):
busts = 0
for i in range(n):
if not simOneHand():
busts = busts + 1
return busts
def simOneHand():
x = simOneCard()
y = simOneCard()
hand = x + y
while hand < 17:
if hasAce(x) is True:
if hand <= 10:
x = 11
hand = hand + 10
else:
x = 1
if hasAce(y) is True:
if hand <= 10:
y = 11
hand = hand + 10
else:
y = 1
z = simOneCard()
hand = hand + z
if hand > 21:
#bust
return False
else:
return True
def simOneCard():
x = randrange(1, 13)
if x == 11 or x == 12 or x == 13:
x = 10
return x
else:
return x
def hasAce(x):
if x == 1:
return True
else:
return False
def printSummary(busts, n):
print()
print("The dealer has a {0:0.1%} chance of busting.".format(busts/n))
if __name__ == '__main__': main()