python random coin toss code example

Example 1: heads or tails python

from random import random

def heads_or_tails():
    guess = input('Pick heads or tails and then press eneter to play: ')
    if random() > 0.5:
        return 'tails'
    else:
        return 'heads'

print(heads_or_tails() + ' wins' + '!')

Example 2: python coin flip

# Import the RANDOM library.
import random as RANDOM # the 'as RANDOM' part could be optionial, 
# it's just choosing a name for the import.

def flip():
  flipped = RANDOM.choice('Heads','Tails')
  print(f'You flipped {flipped}')