Coin - Flip Python Code

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 1

For this task, you'll be creating a Python script to simulate a coin flip.

Your goal is to implement


a program that randomly generates either 'Heads' or 'Tails' with equal probability, mimicking the
outcome of flipping a fair coin. You'll start by importing the 'random' module, which provides
functions for generating random numbers. Using the 'randint' function from the 'random' module,
you'll generate a random integer either 0 or 1, representing the two possible outcomes of a coin
flip. If the generated number is greater than 0.5, the program will print 'Heads'; otherwise, it will
print 'Tails'. This exercise offers an opportunity to practice working with random number
generation and conditional statements in Python.

Coin_flip python code


import random

num = random.randint(0, 1) # Generates a random number that's either 0 or 1

if num > 0.5:


print('Heads')
else:
print('Tails')

This code functions by importing the 'random' module, which enables random number
generation. It then uses the 'randint' function to generate a random integer between 0 and 1,
inclusive. If the generated number is greater than 0.5, indicating a probability of approximately
50%, the program prints 'Heads'; otherwise, it prints 'Tails'. This script effectively simulates the
outcome of flipping a fair coin, providing either 'Heads' or 'Tails' as the result of the coin flip.

You might also like