Rock Paper Scissors in Python - Amazing Mini Project

Today we going to develop Rock Paper Scissors in Python. I did this mini-project when was learning a python language. So, I decide to share code with you. So let’s make Rock Paper Scissors game in Python.

What we are gonna do?

In this game, the user and the computer are the players. First, the code will ask the user to get no. of chances or rounds means how many times you want to play. If the user enters 5 times then code asks 5 times for the choice to enter Rock Paper Scissors.

After entering the number of chances code will ask you to enter one character from list R, P, S for Rock, Paper, Scissors respectively. And at that time computers also generate random character from the list.

After entering the character the player’s score counter will increase in every loop according to game condition to count the score. If both enter the same character then the score counter doesn’t count. And every time code will print the scoreboard with a score of the player.

In the end who gained more score will win the game. If scores are the same then the game is a tie and you have to play again.

Which Python concepts are covered?

  • If-else
  • While Loop
  • String function
  • List
  • Random function

Let’s code

Imports and basic declarations.

import random

print(" ---------------------")
print("| Rock Paper Scissors |")
print(" ---------------------")

listCh = ["R", "P", "S"]

userScore = 0
computerScore = 0
i = 1

chance = int(input("How many time you want to play (no. of chances): "))

The random function takes list listCh as an argument to generate random character from it. userScore and computerScore for count user’s score and computer score respectively, initially 0. Variable i initialize with 1 for while loop. chance stores the no. of chances to play a game (no. of rounds).

Here the main part of the code

while i <= chance:
    computerCh = str(random.choice(listCh))

    userCh = input("Enter Rock, Paper, Scissors (key to press: R,P,S): ").upper()

    if userCh == computerCh:
        print("Tie You Both Entered Same")

    elif userCh == "R":
        print("Computer Enter: ", computerCh)
        if computerCh == "P":
            print("👉 You lose! Paper covers Rock")
            computerScore += 1
        else:
            print("👉 You win! Rock smashes Scissors")
            userScore += 1
    elif userCh == "P":
        print("Computer Enter: ", computerCh)
        if computerCh == "S":
            print("👉 You lose! Scissor cut Paper")
            computerScore += 1
        else:
            print("👉 You win! Paper covers Rock")
            userScore += 1

    elif userCh == "S":
        print("Computer Enter: ", computerCh)
        if computerCh == "R":
            print("👉 You lose! Rock smashes Scissors")
            computerScore += 1
        else:
            print("👉 You win! Scissor cut Paper")
            userScore += 1
    else:
        print(":(")

    print("\n\t******ScoreBoard******")
    print(f"\t You: {userScore} | Computer: {computerScore}")
    print("\t**********************")
    print(f"Game No:[{i}]")
    print("========================================================")

    i += 1

computerCh store the character generated by random function from listCh. userCh stores user input. Note here we use upper() string function to convert lowercase to the uppercase letter. So if the user enters the character in lowercase then it converts it to uppercase.

After getting the user choice, if-else check the conditions according to game rules. As you can see in the above code, if userCh=="R" means users enter R (Rock) and if the condition will become true then code will print the computer-generated character, and inside this if block again checks the condition for a computer. If computerCh=="P" and if the condition will become true then code prints 👉 You lose! Paper covers Rock and computer score will increase by one, computerScore += 1 . And If the condition is false (else part) that means the computer generates the S then code will prints 👉 You win! Rock smashes Scissors and user score will increase by one userScore += 1.

And the same for the user input P and S with the different comparisons with computer input. The above code is inside the while loop. According to no. of chances, this code will be invoked. In every loop (round) code will print the scoreboard with both player’s scores.

Declaring final result

print("\n\n##### Game Over #####")
print("*******************************************")
if userScore < computerScore:
    print(
        "😭 Sorry You lose the game 😭\n computer win the game with score:", computerScore
    )
elif userScore == computerScore:
    print("😅 Game is Tie Play Again 😅")
else:
    print("😄 You Win the Game with score:", userScore, "😄")

After the end of the loop, the code will print the final result. Here we just comparing the userScore and computerScore. Simply, the one who wins the more rounds will win the game. If the scores are the same then the user has to play again.

The full code

import random

print(" ---------------------")
print("| Rock Paper Scissors |")
print(" ---------------------")

listCh = ["R", "P", "S"]

userScore = 0
computerScore = 0
i = 1

chance = int(input("How many time you want to play (no. of chances): "))

while i <= chance:
    computerCh = str(random.choice(listCh))

    userCh = input("Enter Rock, Paper, Scissors (key to press: R,P,S): ").upper()
    if userCh == computerCh:
        print("Tie You Both Entered Same")

    elif userCh == "R":
        print("Computer Enter: ", computerCh)
        if computerCh == "P":
            print("👉 You lose! Paper covers Rock")
            computerScore += 1
        else:
            print("👉 You win! Rock smashes Scissors")
            userScore += 1
    elif userCh == "P":
        print("Computer Enter: ", computerCh)
        if computerCh == "S":
            print("👉 You lose! Scissor cut Paper")
            computerScore += 1
        else:
            print("👉 You win! Paper covers Rock")
            userScore += 1

    elif userCh == "S":
        print("Computer Enter: ", computerCh)
        if computerCh == "R":
            print("👉 You lose! Rock smashes Scissors")
            computerScore += 1
        else:
            print("👉 You win! Scissor cut Paper")
            userScore += 1
    else:
        print(":(")

    print("\n\t******ScoreBoard******")
    print(f"\t You: {userScore} | Computer: {computerScore}")
    print("\t**********************")
    print(f"Game No:[{i}]")
    print("========================================================")

    i += 1

print("\n\n##### Game Over #####")
print("*******************************************")
if userScore < computerScore:
    print(
        "😭 Sorry You lose the game 😭\n computer win the game with score:", computerScore
    )
elif userScore == computerScore:
    print("😅 Game is Tie Play Again 😅")
else:
    print("😄 You Win the Game with score:", userScore, "😄")

# follow @code_snail on instagram

Output

 ---------------------
| Rock Paper Scissors |
 ---------------------
How many time you want to play (no. of chances): 5
Enter Rock, Paper, Scissors (key to press: R,P,S): s
Tie You Both Entered Same

        ******ScoreBoard******
         You: 0 | Computer: 0
        **********************
Game No:[1]
========================================================
Enter Rock, Paper, Scissors (key to press: R,P,S): p
Computer Enter:  S
👉 You lose! Scissor cut Paper

        ******ScoreBoard******
         You: 0 | Computer: 1
        **********************
Game No:[2]
========================================================
Enter Rock, Paper, Scissors (key to press: R,P,S): r
Computer Enter:  S
👉 You win! Rock smashes Scissors

        ******ScoreBoard******
         You: 1 | Computer: 1
        **********************
Game No:[3]
========================================================
Enter Rock, Paper, Scissors (key to press: R,P,S): r
Computer Enter:  S
👉 You win! Rock smashes Scissors

        ******ScoreBoard******
         You: 2 | Computer: 1
        **********************
Game No:[4]
========================================================
Enter Rock, Paper, Scissors (key to press: R,P,S): s
Computer Enter:  P
👉 You win! Scissor cut Paper

        ******ScoreBoard******
         You: 3 | Computer: 1
        **********************
Game No:[5]
========================================================


##### Game Over #####
*******************************************
😄 You Win the Game with score: 3 😄

Hope you like this code. So this the code for Rock Paper Scissors in Python. Stay connected with this blog because I post my all projects and codes here.

see more codes,