My solution to CS Project #1: Python Coin Flip and Dice Guesser

import random

guess_history = [0,0] # win/total

hots = lambda coin: 'heads' if coin == 'h' else 'tails'
resp = lambda guess, side: print('You have guessed correctly!') if guess == side else print('You have guessed incorrectly.')
result = lambda guess, side: 1 if guess == side else 0

def coin_flip(guess):
    global side # side is defined in functions then used outide
    side = random.choice(['h', 't'])
    print('\nYou have guessed ' + hots(guess) + '.\nThe coin has landed on ' + hots(side) + '.') 

def six_sided_dice(guess):
    global side
    side = random.randint(1,6)
    print('\nYou have guessed ' + str(guess) + '.\nThe dice has landed on ' + str(side) + '.') 

while True:
    print('\n[H]eads or [T]ails or [1-6] or [HI]story or [E]xit: ', end='')
    guess = input()
    # Check if guess can be turned into an integer
    try:
        guess = int(guess)
    except ValueError:
        pass
    # Check what option user selects
    if guess in ['h', 't']:
        coin_flip(guess)
    elif guess in list(range(1, 7)):
        six_sided_dice(guess)
    elif guess == 'hi': # Give history based on correct guesses and total guesses
        try:
            print('You have correctly guessed ' + str(guess_history[0]) + ' out of ' + str(guess_history[1]) + ' tries: ' + str(round(100 * guess_history[0] / guess_history[1], 2)) + '%')
        except ZeroDivisionError:
            print('You have correctly guessed 0 out of 0 tries: 0%')
        continue
    elif guess == 'e':
        break
    else:
        print('That is not a choice.')
        continue

    resp(guess, side)
    guess_history[0] +=  result(guess, side)
    guess_history[1] += 1
3 Likes