Advertisement
sriyanto

rock, paper and scissors

Feb 4th, 2024
701
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.99 KB | Gaming | 0 0
  1. import random
  2.  
  3. def get_user_choice():
  4.     # Get user input for their choice, ensure it's valid
  5.     user_choice = input("Enter your choice (Rock, Paper, or Scissors): ").capitalize()
  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, or Scissors): ").capitalize()
  9.     return user_choice
  10.  
  11. def get_computer_choice():
  12.     # Generate a random choice for the computer
  13.     choices = ["Rock", "Paper", "Scissors"]
  14.     return random.choice(choices)
  15.  
  16. def determine_winner(user_choice, computer_choice):
  17.     # Determine the winner based on the game rules
  18.     if user_choice == computer_choice:
  19.         return "It's a tie!", 0
  20.     elif (user_choice == "Rock" and computer_choice == "Scissors") or \
  21.          (user_choice == "Paper" and computer_choice == "Rock") or \
  22.          (user_choice == "Scissors" and computer_choice == "Paper"):
  23.         return "You win!", 1
  24.     else:
  25.         return "Computer wins!", -1
  26.  
  27. def main():
  28.     # Main function to execute the game
  29.     print("Welcome to Rock, Paper, Scissors!")
  30.  
  31.     user_score = 0
  32.     computer_score = 0
  33.     rounds = 0
  34.    
  35.     while True:
  36.         user_choice = get_user_choice()
  37.         computer_choice = get_computer_choice()
  38.  
  39.         print(f"You chose {user_choice}.")
  40.         print(f"Computer chose {computer_choice}.")
  41.  
  42.         result, score_change = determine_winner(user_choice, computer_choice)
  43.         print(result)
  44.  
  45.         user_score += score_change
  46.         computer_score -= score_change
  47.         rounds += 1
  48.  
  49.         print(f"Score after {rounds} rounds: You {user_score} - {computer_score} Computer")
  50.  
  51.         play_again = input("Do you want to play again? (yes/no): ").lower()
  52.         if play_again != "yes":
  53.             print("Thanks for playing! Final score:")
  54.             print(f"You: {user_score} - Computer: {computer_score}")
  55.             break
  56.  
  57. if __name__ == "__main__":
  58.     main()
  59.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement