(Simulation: heads or tails) Write a program that simulates flipping a coin one million times and displays the number of heads and tails.
Simulation: heads or tails Program code:
#Header section
import random
#This method is used to Flips a coin and returns the result
def flipCoinSimulation():
return "heads" if random.random() < 0.5 else "tails"
#main method for simulates flipping a coin one million times
#and displays the number of heads and tails using call the method, flipCoinSimulation
def main():
results = []
for _ in range(1000000):
results.append(flipCoinSimulation())
heads = results.count("heads")
tails = results.count("tails")
print("Heads: {}\nTails: {}".format(heads, tails))
if __name__ == "__main__":
main()
Executed Output 1:
Heads: 500048
Tails: 499952
Executed Output 2:
Heads: 499701
Tails: 500299