DEV Community

Naivedh
Naivedh

Posted on • Updated on

Rock Paper Scissors in Python

Game Rules:
If user_1 and user_2 have same choice there is a TIE.
If choices are Paper and Scissors, Scissors wins.
If choices are Paper and Rock, Paper wins.
If choices are Rock and Scissors, Rock wins.

This small project will help you improve your basics such as loops, nested if...else, variables, random module.

So, guys are you ready to create Rock Paper Scissors using Python. So let's start.

First, we will import the Random module in python.

Alt Text

Now, we will use the while loop to take input until the user wants to play.

Alt Text

Let's take input from the user using input() function in python. Then create the computer choice using randint() function from the Random module.

Alt Text

Now, we will convert the computer's input from 1, 2, 3 into 'p', 'r','s'.

Alt Text

Now, compare the user choice with computer choice using nested if...else. And if both choices are same there is a tie.

Alt Text

Now, take input whether the user wants to play again if no then break the loop.

Alt Text

So, that's it you are ready to play. If you enjoyed, do comment. If you have any doubt feel free to ask.

Top comments (1)

Collapse
 
sojjan1337 profile image
sojjan1337

Instead of converting you could do this:

computer = random.choice(['r', 'p', 's'])