Advertisement
IMustRemainUnknown

Paper Rock Scissors in Python

Dec 11th, 2023
1,113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.42 KB | Source Code | 0 0
  1. import random
  2.  
  3.  
  4. def get_user_choice():
  5.     user_choice = input("Enter your choice (rock, paper, scissors): ").lower()
  6.     while user_choice not in ["rock", "paper", "scissors"]:
  7.         print("Invalid choice. Please choose rock, paper, or scissors.")
  8.         user_choice = input("Enter your choice (rock, paper, scissors): ").lower()
  9.     return user_choice
  10.  
  11.  
  12. def get_computer_choice():
  13.     return random.choice(["rock", "paper", "scissors"])
  14.  
  15.  
  16. def determine_winner(user_choice, computer_choice):
  17.     if user_choice == computer_choice:
  18.         return "It's a tie!"
  19.     elif (user_choice == "rock" and computer_choice == "scissors") or \
  20.          (user_choice == "paper" and computer_choice == "rock") or \
  21.          (user_choice == "scissors" and computer_choice == "paper"):
  22.         return "You win!"
  23.     else:
  24.         return "Computer wins!"
  25.  
  26.  
  27. def main():
  28.     print("Welcome to Rock-Paper-Scissors Game!")
  29.  
  30.     while True:
  31.         user_choice = get_user_choice()
  32.         computer_choice = get_computer_choice()
  33.  
  34.         print(f"You chose {user_choice}")
  35.         print(f"Computer chose {computer_choice}")
  36.  
  37.         result = determine_winner(user_choice, computer_choice)
  38.         print(result)
  39.  
  40.         play_again = input("Do you want to play again? (yes/no): ").lower()
  41.         if play_again != "yes":
  42.             print("Thanks for playing. Goodbye!")
  43.             break
  44.  
  45.  
  46. if __name__ == "__main__":
  47.     main()
  48.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement