Rock Paper Scissors Python..

rock

Rock Paper Scissors is a decision based game and so can easily be replicated as a computer program.

As usual you can write this program in any text editor. I wrote mine in Notepad++

You can name it what ever you want as long as you save it with the .py extension.  I named my file rock.py

You can then run if from the console or by double clicking on the file itself.

You will see \n used throughout the program. This is an instruction to the program to start on a new line. the n will not show up when your program runs.

Now to the program. Being decision based, the if statement playing a starring role. The while loop is used to keep the program running until we decide to close it.

Rock Paper Scissors is a two-person game. We therefore need to create a variable to store the name of each of the the two players. In another post we will look at playing against the computer. For now though we will keep it as a two player game and assign two variables,  one for each player. This is done via an input statement;

 name_one = input(“Please enter name of Player 1 “)

name_two = input(“Please enter name of Player 2 “)

Now we need to ensure the game keeps running until the user decides to close it. For this we use a while loop. We first create an arbitrary variable; var (it can be called anything) and assign it to an arbitrary number; in this case 1 (any number will do).

We use this opportunity to give instructions to the user using a print statement.

var = 1

 print(“Control + C to end at any time… “)

The while loop is designed to keep running while var is equal to 1. In other words, the loop stays open because it’s condition is valid.

while var == 1:

We now create a variable to store the choice of the two players. We give each player a choice of rock paper or scissors. The player will be instructed to pick a number from 1 to 3 to denote their choice. The choice is stored in the respective variables.

player_one = input(“Hi ” + name_one + ”  enter:\n\n 1 for Rock\n 2 for Paper\n 3 for Scissors\n “)

 player_two = input(“Hi ” + name_two +  ”  enter:\n\n 1 for Rock\n 2 for Paper\n 3 for Scissors\n\n “)

Now to the engine of the program. If statements are used to evaluate each of the possible combinations. i.e. Player 1 chooses rock and Player 2 chooses paper etc.

 

 if player_one == “1” and player_two == “2”:

 print(“Paper beats Rock: ” + name_one + ” you are the winner “)

 

elif player_one == “2” and player_two == “1”:

 print(“Paper beats Rock: ” + name_two + ” you are the winner “)

 

if player_one == “2” and player_two == “3”:

print(“Scissors beats Paper ” + name_one + ” you are the winner “)

 

elif player_one == “3” and player_two == “2”:

print(“Scissors beats Paper ” + name_two + ” you are the winner “)

  

if player_one == “1” and player_two == “3”:

 print(“Rock beats Scissors ” + name_one + ” you are the winner “)

 

elif player_one == “3” and player_two == “1”:

print(“Rock beats Scissors ” + name_two + ” you are the winner “)

 

If both players pick the same option; then it is a draw.

elif player_one == player_two:
       print(” Its a draw \n”)

 

Now for the Complete program:
name_one = input(“Please enter name of Player 1 “)
name_two = input(“Please enter name of Player 2 “)
var = 1

print(“Control + C to end at any time…\n”)
while var == 1:

player_one = input(” Hi ” + name_one + ” enter:\n\n 1 for Rock, 2 for Paper, 3 for            Scissors\n\n “)

player_two = input(” Hi ” + name_two + ” enter:\n\n 1 for Rock, 2 for Paper, 3 for Scissors\n\n “)

if player_one == “1” and player_two == “2”:
        print(“Paper beats Rock: ” + name_one + ” you are the winner \n”)

elif player_one == “2” and player_two == “1”:
       print(“Paper beats Rock: ” + name_two + ” you are the winner \n”)

if player_one == “2” and player_two == “3”:
       print(“Scissors beats Paper ” + name_one + ” you are the winner \n”)

elif player_one == “3” and player_two == “2”:
      print(“Scissors beats Paper ” + name_two + ” you are the winner \n”)

if player_one == “1” and player_two == “3”:
       print(“Rock beats Scissors ” + name_one + ” you are the winner \n”)

elif player_one == “3” and player_two == “1”:
       print(“Rock beats Scissors ” + name_two + ” you are the winner \n”)

elif player_one == player_two:
       print(” Its a draw \n”)

 

 

 

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s