8000 added new solution for coin_toss · coderwxy/book1-exercises@fc8e156 · GitHub
[go: up one dir, main page]

Skip to content

Commit fc8e156

Browse files
committed
added new solution for coin_toss
1 parent 4991e5d commit fc8e156

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from random import randint
2+
3+
def single_trial():
4+
toss = randint(0, 1)
5+
total_flips = 1
6+
7+
while toss == randint(0, 1):
8+
total_flips += 1
9+
toss = randint(0, 1)
10+
11+
total_flips += 1
12+
return total_flips
13+
14+
def flip_trial_avg(num_trials):
15+
total = 0
16+
for trial in range(num_trials):
17+
total += single_trial()
18+
return total/num_trials
19+
20+
print("The average number of coin flips was {0}".format(flip_trial_avg(10000)))

0 commit comments

Comments
 (0)
0