

Each is largely independent, fixing a different issue with the code. The following is a brief explanation of these changes. Print('Chance of streak: %s%%' % (numberOfStreaks / 100.0)) # this makes some sense, but is likely not what the book's author intends # if we don't, we get percentages above 100, e.g. Streak = 1 # (2) any flip is a streak of (at least) 1īreak # (5) we've found a streak in this CoinFlip list, skip to next experiment Streak = 1 # (2, 4) any flip is a streak of (at least) 1 reset for next checkįor i in range(1, len(CoinFlip)): # (3) start at the second flip, as we will look back 1 import randomĬoinFlip = # (1) create a new, empty list for this list of 100 I have marked modifications with comments prefixed by # and numbered them with reference to the explanations that follow.
#COIN FLIP ODDS CODE#
The following is a set of minor modifications to the initially provided code that will compute the estimate correctly. Output: Chance of streak: 0.7834% rather than.

Print('Chance of streak: %s%%' % chance_percent) #function that converts decimal to percent and rounds # Code that creates a list of 100 'heads' or 'tails' values.'Ĭhance = (numberOfStreaks / total_instances) Lastly, changed the hard coding to variables, obviously doesn't matter but just to explain the things I changed. Therefore, I added a function that converts a decimal to percentage and rounds it to 4 decimal places. I was just working on the same problem in my own learning process.įirst, I was unsure why you used multiple for loops when the range was the same length, so I combined those and continued to get the same results.Īlso, I noticed that the final calculation is presented as a percentage but not converted to a percentage from the original calculation.įor example, 5/100 =. I am new to programming so anyone please correct me if I'm wrong. I wasn't able to comment on Stuart's answer because I recently joined and don't have the reputation, so that's why this an answer on it's own. I'm not sure why their "hint" suggest dividing by only 100. I also think you need to divide by 100*10000 to get the real probability. for experimentNumber in range(10000):Įlif CoinFlip = CoinFlip: #checks if current list item is the same as before I also added a check for i=0 so that you're not comparing to the end of the list, because that's not technically part of the streak. Your current program just keeps appending to CoinFlip, which makes for a very long list. Where did I make the mess? I can't really see it! Print('Chance of streak: %s%%' % (numberOfStreaks / 100))

If CoinFlip = CoinFlip: #checks if current list item is the same as before # Code that checks if there is a streak of 6 heads or tails in a row. I am going to check if there is multiple 0 or 1 in a row #does not matter if it is 0 or 1, H or T, peas or lentils. # Code that creates a list of 100 'heads' or 'tails' values. Do I have a problem with the first iteration of the comparison of current value to value before (as far as I understand it, I would compare index -1 (which then is index 100?) to the current one?) import random I tried to be as lazy as possible which results in my macbook working really hard. Therefore, if you guys could just point out the errors in the code, that would be nice :) But in previous questions the coding problem was described as a bit fuzzy. The task is to to 10.000 samples of 100 flips each and then compute the probability of a 6x heads or tails streak over all the samples - as far as I understand it.

I know there's tons of questions about it by now, even for the same problem, but I think I tried a bit of a different approach.
