Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # https://automatetheboringstuff.com/2e/chapter4/
- # project 2 Coin Flip Streaks ver. 2
- import random
- numberOfStreaks = 0
- runs = 10000
- flips_per_run = 100
- streakNo = 6 # how many streaks to look for
- streaksCounter = 0 # counts consequtive strikes
- currentIndex = None # records current index in flipsList
- nextIndex = None # records next index in flipsList
- for i in range(runs):
- # Code that creates a list of 100 'heads' or 'tails' values.
- flipsList = []
- for i in range(flips_per_run):
- if random.randint(0,1) == 1:
- flipsList.append('H')
- else:
- flipsList.append('T')
- # Code that checks if there is a streak of 6 heads or tails in a row.
- for i in range(len(flipsList)):
- currentIndex = nextIndex
- nextIndex = flipsList[i]
- if currentIndex == nextIndex:
- streaksCounter += 1
- else:
- streaksCounter = 0
- if streaksCounter == streakNo:
- numberOfStreaks += 1
- print('numberOfStreaks is: ' + str(numberOfStreaks))
- print('Chance of streak: %s%%' % (numberOfStreaks / runs))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement