Rock Paper Scissor Game With Python

Rock Paper Scissor Game With Python

Hello friends. What's up? Hope doing well. Today we are going to make a simple but very interesting game with python. It is a childhood game called " Rock Paper Scissor ". So let's start this.......


First of all trust me this is very easy. Just you should have the basic knowledge about python or any other programming language, and that's it!!!!!!

At first you have to install Python on your computer. For that click HERE, download and install python. This is very easy to install python. 

Then I am using PyCharm to edit the code and also run the code. You can use Visual Code Studio, Sublime Text Editor or Python IDLE etc.. as your wish...
You can download and install PyCharm from HERE ... :)

Now let's come to the coding part.
First I have imported random module. It is preinstalled with python. Then I have taken two variables called comp_score, player_score which initial values are 0. And then I have just welcomed the player with print() function.



import random

comp_score = 0
player_score = 0

print("WELCOME")

Then I have made a function called Choose_option() which will take input from user. I have used only if else and elif condition to take inputs from user.


def Choose_Option():
    user_choice = input("Choose Rock, Paper or Scissors: ")
    if user_choice in ["Rock", "rock", "r", "R", "ROCK"]:
        user_choice = "r"
    elif user_choice in ["Paper", "paper", "p", "P", "PAPER"]:
        user_choice = "p"
    elif user_choice in ["Scissors", "scissors", "s", "S", "SCISSORS"]:
        user_choice = "s"
    else:
        print("I don't understand, try again.")
        Choose_Option()
    return user_choice

Then I have made an another function called Computer_option() which will take input from computer. I have used only if else and elif condition to take inputs from computer. In this case computer will generate a random number from 1 to 3. And then the generated number will be the input from computer.


def Computer_Option():
    comp_choice = random.randint(1, 3)
    if comp_choice == 1:
        comp_choice = "r"
    elif comp_choice == 2:
        comp_choice = "p"
    else:
        comp_choice = "s"
    return comp_choice

Then I have created an infinite while loop. Here I have taken inputs from user and computer and then compared the inputs. Then with sum condition I have started to find out if there is a tie or computer win or player win. Then I have distributed the scores among player and computer.

while True:
    #print("")

    user_choice = Choose_Option()
    comp_choice = Computer_Option()

    #print("")

    if user_choice == "r":
        if comp_choice == "r":
            print("You chose rock. The computer chose rock. You tied.")

        elif comp_choice == "p":
            print("You chose rock. The computer chose paper. You lose.")
            comp_score += 1

        elif comp_choice == "s":
            print("You chose rock. The computer chose scissors. You win.")
            player_score += 1

    elif user_choice == "p":
        if comp_choice == "r":
            print("You chose paper. The computer chose rock. You win.")
            player_score += 1

        elif comp_choice == "p":
            print("You chose paper. The computer chose paper. You tied.")


        elif comp_choice == "s":
            print("You chose paper. The computer chose scissors. You lose.")
            comp_score += 1

    elif user_choice == "s":
        if comp_choice == "r":
            print("You chose scissors. The computer chose rock. You lose.")
            comp_score += 1

        elif comp_choice == "p":
            print("You chose scissors. The computer chose paper. You win.")
            player_score += 1

        elif comp_choice == "s":
            print("You chose scissors. The computer chose scissors. You tied.")

    print("")
    print("Player score: " + str(player_score))
    print("Computer score: " + str(comp_score))
    print("")

Then at last of the loop I have Created a condition. If anyone scores 10 then this will break the loop.
Then with another condition it will find out the winner


    if player_score == 10 or comp_score == 10:
        break

print("Game end....... Let's see the result")

if player_score > comp_score:
    print("Congratulation :)\nYou won the game.")

elif player_score == comp_score:
    print("This is a tie")

else:
    print("You lost the match. Try again")


And now our game is fully ready. Enjoy the game :)

Here is the full code-


'''Created By
Zubaer Mahmud
www.screwdrvr.blogspot.com
'''
import random

comp_score = 0
player_score = 0

print("WELCOME")

def Choose_Option():
    user_choice = input("Choose Rock, Paper or Scissors: ")
    if user_choice in ["Rock", "rock", "r", "R", "ROCK"]:
        user_choice = "r"
    elif user_choice in ["Paper", "paper", "p", "P", "PAPER"]:
        user_choice = "p"
    elif user_choice in ["Scissors", "scissors", "s", "S", "SCISSORS"]:
        user_choice = "s"
    else:
        print("I don't understand, try again.")
        Choose_Option()
    return user_choice


def Computer_Option():
    comp_choice = random.randint(1, 3)
    if comp_choice == 1:
        comp_choice = "r"
    elif comp_choice == 2:
        comp_choice = "p"
    else:
        comp_choice = "s"
    return comp_choice


while True:
    #print("")

    user_choice = Choose_Option()
    comp_choice = Computer_Option()

    #print("")

    if user_choice == "r":
        if comp_choice == "r":
            print("You chose rock. The computer chose rock. You tied.")

        elif comp_choice == "p":
            print("You chose rock. The computer chose paper. You lose.")
            comp_score += 1

        elif comp_choice == "s":
            print("You chose rock. The computer chose scissors. You win.")
            player_score += 1

    elif user_choice == "p":
        if comp_choice == "r":
            print("You chose paper. The computer chose rock. You win.")
            player_score += 1

        elif comp_choice == "p":
            print("You chose paper. The computer chose paper. You tied.")


        elif comp_choice == "s":
            print("You chose paper. The computer chose scissors. You lose.")
            comp_score += 1

    elif user_choice == "s":
        if comp_choice == "r":
            print("You chose scissors. The computer chose rock. You lose.")
            comp_score += 1

        elif comp_choice == "p":
            print("You chose scissors. The computer chose paper. You win.")
            player_score += 1

        elif comp_choice == "s":
            print("You chose scissors. The computer chose scissors. You tied.")

    print("")
    print("Player score: " + str(player_score))
    print("Computer score: " + str(comp_score))
    print("")

    if player_score == 10 or comp_score == 10:
        break

print("Game end....... Let's see the result")

if player_score > comp_score:
    print("Congratulation :)\nYou won the game.")

elif player_score == comp_score:
    print("This is a tie")

else:
    print("You lost the match. Try again")

You can download the code from HERE


SHARE THIS

Author:

Previous Post
Next Post