Python program that simulates a coin flip.

Write a program that simulates a coin flip. The program displays a menu as shown in the sample run. You can enter 1 or 2 for choosing Head or Tail. After a first flip is finished, the menu is redisplayed. You may choose 9 to exit the program. Each flip generates a random number between 1 (Tail) and 2 (Head). Before you exit the program, print number of coin flips and number of times the user choice was correct.

Sample output:

Enter 1 for Head

Enter 2 for Tail

Enter 9 to exit
2

You Lost

 

Enter 1 for Head

Enter 2 for Tail

Enter 9 to exit
1
You Won


Enter 1 for Head

Enter 2 for Tail

Enter 9 to exit
2
You Won


Enter 1 for Head

Enter 2 for Tail

Enter 9 to exit
2
You Lost


Enter 1 for Head

Enter 2 for Tail

Enter 9 to exit
2
You Lost


Enter 1 for Head

Enter 2 for Tail

Enter 9 to exit
1
You Won


Enter 1 for Head

Enter 2 for Tail

Enter 9 to exit
9


Number of flips 6 You Won 3 Time(s)

import random
num = 0
win = 0

while (True):
     print(“Enter 1 for Head”)
     print(“Enter 2 for Tail”)
     print(“Enter 9 to exit”)
     answer = int(input())
     flip = random.randint(1,2)
     if (answer == flip):
         num += 1
         win += 1
         print(“You Won”)
     elif (answer == 9):
         print (“number of flip “+ str(num))
         print (“You won “+str(win)+” times”)
         break
     else:
         num += 1
         print(“You Lost”)

P