Coin Flip Streaks from (Automate the Boring Stuff with Python Practical Programming for Total Beginners)

 Coin Flip Exercise:

Write a program to find out how often a streak of six heads or a streak of six tails comes up in a randomly generated list of heads and tails from 10000 flips and sorted every 100 in one list.

code:

import random
strickheads= 0
stricktials=0
k=[[]for c in range(100)]
x=[]
for e in range(100) :
for i in range(100):
a=random.randint(0,1)
if a==1:
k[e]+='H'
else:
k[e]+='T'
print(k[e])
#part two of the question
for i in range(100):
x+=k[i]
#checking
for i in range(10000-5):
if x[i]==x[i+1]==x[i+2]==x[i+3]==x[i+4]==x[i+5]=='H':
strickheads+=1
elif x[i] == x[i + 1] == x[i + 2] == x[i + 3] == x[i + 4] == x[i + 5] == 'T':
stricktials+=1
print('NO. of 6 heads= ',strickheads)
print('NO. of 6 tails= ',stricktials)

Post a Comment

Please Select Embedded Mode To Show The Comment System.*

Previous Post Next Post