This script will allow you to simulate flipping a coin a specified number of times.
import random # Create a function that simulates # flipping a coin a specific number of times def coin_flip_simulation(num_flips): heads = 0 tails = 0 for _ in range(num_flips): if random.choice(['Heads', 'Tails']) == 'Heads': heads += 1 else: tails += 1 return heads, tails while True: try: # Informing the user they can type 'exit' to stop print("Type 'exit' to exit script.") # User input for the number of flips or an exit command user_input = input("# of coin flips: ").strip() if user_input.lower() == 'exit': print("Exiting the script. Goodbye!") break num_flips = int(user_input) # Simulate coin flips heads, tails = coin_flip_simulation(num_flips) # Display the results in text print("\nResults after {} coin flips:".format(num_flips)) print("Heads: {}".format(heads)) print("Tails: {}".format(tails)) # Display an error message if the input is not a valid number except ValueError: print("Please enter a valid number or type 'exit' to stop.")