Back

Code Challenge: Coin Flip Simulation

Difficulty  

divider

Objective

Flip a coin as many times as the user asks, then report how many heads and tails came up and what percentage each was.

Heads or Tails was a game. This is an experiment — the interesting part is whether 1000 flips really do land near 50/50. Try it and find out.

Skills to Practice

  • Repeating an action a set number of times with range()
  • Generating a random result
  • Using counter variables
  • Calculating a percentage

Tasks

  • Create a new Python program named cc-coin-flip-simulation.
  • Prompt the user for how many times to flip the coin.
  • Use a loop to flip that many times, showing the result of each flip.
  • Count how many heads and how many tails occurred.
  • Calculate what percentage of the flips were heads, and what percentage were tails.
  • Display the totals and the percentages.

Once it works, run it with 10 flips, then 100, then 1000. The percentages should settle closer to 50/50 the more flips you do.


Sample Output

Sample Output
Enter the number of times to flip the coin: 10 [Enter]
Flip 1: Heads
Flip 2: Tails
Flip 3: Heads
Flip 4: Heads
Flip 5: Tails
Flip 6: Heads
Flip 7: Tails
Flip 8: Tails
Flip 9: Heads
Flip 10: Heads
Results:
Heads: 6 (60.0%)
Tails: 4 (40.0%)

Commence Challenge