import random # function to flip the coin def coin_flip(): # generate a random number between 0 and 1 num = random.randint(0, 1) if num == 0: return 'Heads' else: return 'Tails' # main game loop while True: print('Welcome to the coin flip game!') guess = input('Guess Heads or Tails: ') # check if the input is valid if guess.lower() not in ['heads', 'tails']: print('Invalid input. Please try again.') continue # flip the coin and print the result result = coin_flip() print('The result is:', result) # check if the guess is correct and print the outcome if guess.lower() == result.lower(): print('Congratulations, you win!') else: print('Sorry, you lose.') # ask the player if they want to play again play_again = input('Do you want to play again? (Y/N): ') if play_again.lower() != 'y': break print('Thanks for playing!')