Skip to main content

Section 24.5 Building a Simulation - 2

Now that we have a working function, we can focus on using the function. Look for the spot with the missing code at line 28. Add the needed code to complete the accumulator pattern to count up the number of times result ends up as True. The expected answer is around 80% - about 8 times out of 10 you should get at least 5 heads in a row if you flip a coin 100 times.
If you run into troubles, here are some debugging tips:
  • We know checkHeadsInRow works. Focus your attention on the main part of the program.
  • Print the result you get in each repetition loop.
  • Print the successCounter at the end of each repetition of the loop.
  • Reduce repetitions to something like 10 to make it easier to see all the output. Your answer will be less reliable with fewer repetitions, so reset it to 1000 when you are done debugging.

Checkpoint 24.5.1.

Make sure that your simulation is giving the right answer for the likelihood of getting 5 heads in a row while flipping a coin 100 times. You should be getting an answer of around 80%.
Then modify the program to check the odds of getting 6 heads in a row (instead of just 5). What answer do you get? Round it to the nearest whole number and enter it here:
Tip: you may want to change repetitions to 10000 so your answer is more reliable (the more repetitions you do, the more reliable the result will be.)
You have attempted of activities on this page.