Advertisement
nikolask

Coin Flip Streaks ver. 2 // absp

Apr 12th, 2020
439
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.18 KB | None | 0 0
  1. # https://automatetheboringstuff.com/2e/chapter4/
  2. # project 2 Coin Flip Streaks ver. 2
  3.  
  4. import random
  5.  
  6. numberOfStreaks = 0
  7. runs = 10000
  8. flips_per_run = 100
  9. streakNo = 6                        # how many streaks to look for
  10.  
  11. streaksCounter = 0                  # counts consequtive strikes
  12. currentIndex = None                 # records current index in flipsList
  13. nextIndex = None                    # records next index in flipsList
  14.  
  15. for i in range(runs):
  16.     # Code that creates a list of 100 'heads' or 'tails' values.
  17.     flipsList = []
  18.     for i in range(flips_per_run):
  19.         if random.randint(0,1) == 1:
  20.             flipsList.append('H')
  21.         else:
  22.             flipsList.append('T')
  23.  
  24.     # Code that checks if there is a streak of 6 heads or tails in a row.
  25.     for i in range(len(flipsList)):
  26.         currentIndex = nextIndex
  27.         nextIndex = flipsList[i]
  28.         if currentIndex == nextIndex:
  29.             streaksCounter += 1
  30.         else:
  31.             streaksCounter = 0
  32.         if streaksCounter == streakNo:
  33.             numberOfStreaks += 1
  34.        
  35. print('numberOfStreaks is: ' + str(numberOfStreaks))
  36. print('Chance of streak: %s%%' % (numberOfStreaks / runs))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement