The classic game of Rock, Paper, Scissors is a timeless activity that has been enjoyed by people of all ages. It’s a simple yet exciting game that can be used to make decisions, settle disputes, or just have fun. In this blog, we’ll dive into how you can build your own Rock, Paper, Scissors game using Python. We’ll provide you with the complete code, explain how it works, and guide you through the logic behind the game. So, let’s get started!
The Rules of the Game
Before we dive into the code, let’s quickly recap the rules of Rock, Paper, Scissors:
- Rock smashes scissors.
- Paper covers rock.
- Scissors cut paper.
Each player chooses one of the three options, and the winner is determined by the rules above. If both players choose the same option, the round is a tie.
Building the Game with Python
To create our Rock, Paper, Scissors game, we’ll use Python and its random
module to simulate the computer’s choice. We’ll also write functions to get the user’s choice, determine the winner of each round, and keep track of the scores. Let’s walk through each part of the code to understand how it works:
1. Importing the random
Module
We start by importing the random
module, which we’ll use to randomly select the computer’s choice.
import random
Getting the Computer’s Choice
The get_computer_choice
function randomly selects one of the three options: rock, paper, or scissors.
def get_computer_choice():
choices = ["rock", "paper", "scissors"]
return random.choice(choices)
Getting the User’s Choice
The get_user_choice
function prompts the user to enter their choice. It ensures that the input is valid by using a while loop.
def get_user_choice():
while True:
user_input = input("Enter your choice (rock, paper, or scissors): ").lower()
if user_input in ["rock", "paper", "scissors"]:
return user_input
else:
print("Invalid choice. Please try again.")
Determining the Winner
The determine_winner
function compares the user’s choice with the computer’s choice and determines the winner based on the rules of the game.
def determine_winner(user_choice, computer_choice):
if user_choice == computer_choice:
return "tie"
if ((user_choice == "rock" and computer_choice == "scissors") or
(user_choice == "scissors" and computer_choice == "paper") or
(user_choice == "paper" and computer_choice == "rock")):
return "user"
else:
return "computer"
Playing the Game
The play_game
function runs the main loop of the game. It keeps track of the scores, displays the choices and results, and asks the user if they want to play again.
def play_game():
user_score = 0
computer_score = 0
while True:
user_choice = get_user_choice()
computer_choice = get_computer_choice()
print(f"You Chose: {user_choice}")
print(f"The computer chose: {computer_choice}")
result = determine_winner(user_choice, computer_choice)
if result == "user":
print("You win this round!")
user_score += 2
elif result == "computer":
print("The computer wins this round!")
computer_score += 2
else:
print("It's a tie!")
print(f"Current Score - You: {user_score}, Computer: {computer_score}")
play_again = input("Do you want to play again? (yes/no): ").lower()
if play_again != "yes":
break
if user_score > computer_score:
print(f"Final result: You win the game! Final Score - You: {user_score}, Computer: {computer_score}")
elif computer_score > user_score:
print(f"Final result: The Computer wins the game! Final Score - You: {user_score}, Computer: {computer_score}")
else:
print(f"Final result: It's a tie game! Final Score - You: {user_score}, Computer: {computer_score}")
Starting the Game
Finally, we use the if __name__ == "__main__":
block to ensure that the game starts when the script is run.
if __name__ == "__main__":
play_game()
Conclusion:
Congratulations! You’ve built your very own Rock, Paper, Scissors game in Python. This project is a great way to practice your programming skills and have some fun along the way. You can enhance this game further by adding more features, such as a graphical user interface or a scoring system that tracks multiple sessions.
This tutorial exemplifies the power of Python libraries in creating practical solutions. Whether you’re a developer honing your skills or an individual seeking personalized task management, this guide offers a solid starting point. Moving forward, feel free to explore further customization and feature additions to tailor the app to your specific needs. With creativity and innovation, the potential for enhancement is limitless.
If you have any queries related to this article, then you can ask in the comment section, we will contact you soon, and Thank you for reading this article.
Follow me to receive more useful content:
Instagram | Twitter | Linkedin | Youtube
Thank you