quhluh

Coin Flip Game

Mar 30th, 2023
365
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.98 KB | None | 0 0
  1. import random
  2.  
  3. # function to flip the coin
  4. def coin_flip():
  5.     # generate a random number between 0 and 1
  6.     num = random.randint(0, 1)
  7.     if num == 0:
  8.         return 'Heads'
  9.     else:
  10.         return 'Tails'
  11.  
  12. # main game loop
  13. while True:
  14.     print('Welcome to the coin flip game!')
  15.     guess = input('Guess Heads or Tails: ')
  16.    
  17.     # check if the input is valid
  18.     if guess.lower() not in ['heads', 'tails']:
  19.         print('Invalid input. Please try again.')
  20.         continue
  21.    
  22.     # flip the coin and print the result
  23.     result = coin_flip()
  24.     print('The result is:', result)
  25.    
  26.     # check if the guess is correct and print the outcome
  27.     if guess.lower() == result.lower():
  28.         print('Congratulations, you win!')
  29.     else:
  30.         print('Sorry, you lose.')
  31.    
  32.     # ask the player if they want to play again
  33.     play_again = input('Do you want to play again? (Y/N): ')
  34.     if play_again.lower() != 'y':
  35.         break
  36.  
  37. print('Thanks for playing!')
Advertisement
Add Comment
Please, Sign In to add comment