Rock-Paper-Scissors is a Python project where player selects one of three options: Rock, Paper, or Scissors. The computer simultaneously makes a random selection. The program compares both choices using the game rules and declares the winner. The game can be played repeatedly until the user decides to quit.
Example:
1. User Choice: Rock
Computer Choice: ScissorsResult: User Wins
2. User Choice: Paper
Computer Choice: PaperResult: Draw
Winning Rules
- Rock defeats Scissors.
- Scissors defeats Paper.
- Paper defeats Rock.
- If both players choose the same option, the round ends in a draw.
In this game, randint() built-in function is used for generating random integer values within the given range.
Algorithm
- Display the rules of the game.
- Accept the user's choice.
- Validate the input and ensure it is within the allowed options.
- Generate the computer's choice randomly.
- Compare both choices and determine the winner.
- Display the result of the round.
- Ask the user whether they want to play again.
- Repeat the game until the user chooses to exit.
Implementation
import random
# Display game rules
print("Welcome to Rock-Paper-Scissors!\n")
print("Winning Rules:")
print("Rock vs Paper -> Paper wins")
print("Rock vs Scissors -> Rock wins")
print("Paper vs Scissors -> Scissors wins\n")
choices = ["Rock", "Paper", "Scissors"]
while True:
print("Choose an option:")
print("1 - Rock")
print("2 - Paper")
print("3 - Scissors")
# Validate user input
try:
choice = int(input("Enter your choice: "))
except ValueError:
print("Please enter a valid number.\n")
continue
while choice < 1 or choice > 3:
choice = int(input("Please enter a valid choice (1-3): "))
# User choice
user_choice = choices[choice - 1]
print("\nUser choice is:", user_choice)
print("Now it's Computer's Turn...")
# Computer choice
comp_choice = random.randint(1, 3)
computer_choice = choices[comp_choice - 1]
print("Computer choice is:", computer_choice)
print(user_choice, "vs", computer_choice)
# Determine winner
if choice == comp_choice:
print("<== It's a Tie! ==>")
elif (
(choice == 1 and comp_choice == 3) or
(choice == 2 and comp_choice == 1) or
(choice == 3 and comp_choice == 2)
):
print("<== User Wins! ==>")
else:
print("<== Computer Wins! ==>")
# Play again
while True:
ans = input("\nDo you want to play again? (Y/N): ").lower()
if ans in ['y', 'n']:
break
print("Please enter Y or N.")
if ans == 'n':
break
print()
print("\nThanks for playing!")
Output:
Winning rules of the game ROCK PAPER SCISSORS are:
Rock vs Paper -> Paper wins
Rock vs Scissors -> Rock wins
Paper vs Scissors -> Scissors wins
Enter your choice
1 - Rock
2 - Paper
3 - Scissors
Enter your choice:
1 - Rock
2 - Paper
3 - Scissors
Enter your choice: 1
User choice is: Rock
Computer choice is: Scissors
<== User wins! ==>
Do you want to play again? (Y/N)
N
Thanks for playing!
Explanation:
- Store Available Choices: A list is used to store the three possible options: Rock, Paper, and Scissors.
- Validate User Input: The program uses try-except and conditional checks to ensure the user enters a valid choice.
- Generate Random Selection: The random.randint() function generates a random choice for the computer.
- Map Numbers to Choices: User and computer selections are converted from numeric values to their corresponding string representations.
- Determine the Winner: Conditional statements compare the selected options and identify the winner or a draw.
- Control Game Execution: A while loop allows multiple rounds to be played until the user chooses to exit the game.