"Learn to build a simple Rock, Paper, Scissors game in Python with this beginner-friendly guide."
By Samir Niroula
28 October 2024Rock, Paper, Scissors is a fun, quick game and a great beginner Python project. This guide will walk you through building a simple text-based version. Let’s jump in!
First, we’ll use Python’s built-in random
module to let the computer make random choices.
Code:
import random
Explanation: The random module will allow us to randomly pick Rock, Paper, or Scissors for the computer’s move.
We’ll define the available choices (Rock, Paper, Scissors) in a list so we can access them easily.
Code:
choices = ["rock", "paper", "scissors"]
Explanation: Each item in the list represents a possible choice for both the player and the computer.
Let’s write a function to get the player’s choice and ensure they enter a valid option.
Code:
def get_player_choice():
player_choice = input("Enter rock, paper, or scissors: ").lower()
while player_choice not in choices:
print("Invalid choice. Please try again.")
player_choice = input("Enter rock, paper, or scissors: ").lower()
return player_choice
Explanation: This function asks the player for their choice and checks if it’s valid. If it’s not, it asks again until a valid input is provided.
Now, let’s get a random choice for the computer using random.choice().
Code:
def get_computer_choice():
return random.choice(choices)
Explanation: This function randomly selects Rock, Paper, or Scissors from our choices list for the computer.
We’ll create a function to compare the player’s choice with the computer’s choice and decide the winner.
Code:
def determine_winner(player, computer):
if player == computer:
return "It's a tie!"
elif (player == "rock" and computer == "scissors") or \
(player == "scissors" and computer == "paper") or \
(player == "paper" and computer == "rock"):
return "You win!"
else:
return "Computer wins!"
**Explanation:** This function checks all possible winning conditions for the player. If none match, the computer wins.
Finally, we’ll put everything together in a main function that runs the game.
Code:
def play_game():
print("Welcome to Rock, Paper, Scissors!")
player_choice = get_player_choice()
computer_choice = get_computer_choice()
print(f"You chose: {player_choice}")
print(f"Computer chose: {computer_choice}")
result = determine_winner(player_choice, computer_choice)
print(result)
Explanation: This function runs the entire game: it gets the player’s and computer’s choices, shows both choices, and displays the result.
If you want, you can add a loop to allow the player to play multiple rounds.
Code:
while True:
play_game()
play_again = input("Do you want to play again? (yes/no): ").lower()
if play_again != "yes":
print("Thanks for playing!")
break
Explanation: This loop runs play_game() repeatedly until the player chooses not to play again.
Here’s the complete code for the game:
import random
choices = ["rock", "paper", "scissors"]
def get_player_choice():
player_choice = input("Enter rock, paper, or scissors: ").lower()
while player_choice not in choices:
print("Invalid choice. Please try again.")
player_choice = input("Enter rock, paper, or scissors: ").lower()
return player_choice
def get_computer_choice():
return random.choice(choices)
def determine_winner(player, computer):
if player == computer:
return "It's a tie!"
elif (player == "rock" and computer == "scissors") or \
(player == "scissors" and computer == "paper") or \
(player == "paper" and computer == "rock"):
return "You win!"
else:
return "Computer wins!"
def play_game():
print("Welcome to Rock, Paper, Scissors!")
player_choice = get_player_choice()
computer_choice = get_computer_choice()
print(f"You chose: {player_choice}")
print(f"Computer chose: {computer_choice}")
result = determine_winner(player_choice, computer_choice)
print(result)
while True:
play_game()
play_again = input("Do you want to play again? (yes/no): ").lower()
if play_again != "yes":
print("Thanks for playing!")
break
Output
Welcome to Rock, Paper, Scissors!
Enter rock, paper, or scissors: rock
You chose: rock
Computer chose: scissors
You win!
Do you want to play again? (yes/no): yes
Welcome to Rock, Paper, Scissors!
Enter rock, paper, or scissors: paper
You chose: paper
Computer chose: rock
You win!
Do you want to play again? (yes/no): yes
Welcome to Rock, Paper, Scissors!
Enter rock, paper, or scissors: scissors
You chose: scissors
Computer chose: scissors
It's a tie!
Do you want to play again? (yes/no): no
Thanks for playing!
Congratulations! You’ve created a Rock, Paper, Scissors game in Python. This project helps beginners learn user input, conditional statements, and basic functions. Keep experimenting with the game to add more features and improve your skills!