DEV Community

loading...
Cover image for How to build Rock Paper Scissors Game in Python

How to build Rock Paper Scissors Game in Python

mindninjax profile image Rishabh Singh ⚡ ・3 min read

Hola everyone! Today we will be creating a fun and amazing Rock Paper Scissors game in Python!

Alt Text

How does it work?

Our game will be based on the rules of the original Rock Paper Scissors game. We will be asking for input to the user and then we will ask the computer to choose between Rock, Paper & Scissors randomly. Then we will compare the user input with the computer made choice and then see who is the winner through basic conditional comparisons.

Let's Code

Alright so, as usual, we are going to import our favourite module random to get started.

import random
Enter fullscreen mode Exit fullscreen mode

Now we are going to create a simple function which will do the important job of comparing the input by the user and the choice made by the computer to find out the winner.

def check_win(user, computer):
    if (user == 'r' and computer == 's') or (user == 's' and computer == 'p') or (user == 'p' and computer == 'r'):
        return True
Enter fullscreen mode Exit fullscreen mode

Let's give it the name check_win() and it will take two parameters, user parameter represents the input provided by user & computer represents the choice made by our machine.

Within the function, it uses one if statement which uses and & or operator to find the winner. I guess this part is clear enough for anyone who has played Rock Paper Scissors before.

This if statement will check if the user is winning or not, if the user is winning then it will return a True else nothing.

Now let's move on to the next function.

def rock_paper_scissors():
    player = input("What is your choice - 'r' for rock, 's' for scissor, 'p' for paper: ")
    choices = ['r','s','p']
    opponent = random.choice(choices)

    if player == opponent:
        return print(f"Its a Tie! Choice is {opponent}")

    if check_win(player, opponent):
        return print(f"Yay! you won! Choice is {opponent}")

    if check_win(player, opponent) != True:
        return print(f"You lost! Choice is {opponent}")
Enter fullscreen mode Exit fullscreen mode

Here we have created an function named rock_paper_scissors(). This function is going to be the core of our game.

Let's break it down & try to understand what is going on here.

First, we are asking the choice of the user as an input. Here the user can provide input through only **r**, **s** or **p** which resembles rock, scissor & paper respectively.

Now we have the user input stored in player variable.

It's the turn of our machine to make a choice for rock, scissor & paper. For that we have created a list choices which contains three elements **r**, **s** or **p.**

Now we are going to make use of random.choice() function to randomly choose a single element from the list choices.

Finally, we have stated 3 **if** statements which accomplish the following:

  • The first **if** statement, comparison between player & opponent is done to call it a tie if the choice of user and computer is the same. In this case, there is no winner.
  • In second if statement, we are using our check_win() function to decide the winner. If the winner has won, the check_win() will return a True and in that case the second if statement will execute and the user will be declared as the winner.
  • The third if statement works in the same way as the second if statement, the only difference is that it will be executed if the check_win() returned nothing which it will not if the computer is the winner. In this case, the computer will be declared as the winner.

Now finally to run our code, let's call our function rock_paper_scissors().

rock_paper_scissors()
Enter fullscreen mode Exit fullscreen mode

Source Code

You can find the complete source code of this project here -

mindninjaX/Python-Projects-for-Beginners

Support

Thank you so much for reading! I hope you found this beginner project useful.

If you like my work please consider Buying me a Coffee so that I can bring more projects, more articles for you.

https://dev-to-uploads.s3.amazonaws.com/i/5irx7eny4412etlwnc64.png

Also if you have any questions or doubts feel free to contact me on Twitter, LinkedIn & GitHub. Or you can also post a comment/discussion & I will try my best to help you :D

Discussion (0)

pic
Editor guide