## DEV Community is a community of 600,578 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

# How to build Rock Paper Scissors Game in Python

Rishabh Singh ⚡ ・3 min read

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

## 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
``````

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
``````

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}")
``````

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()
``````

## 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.