So, let’s begin with the opening of the Spyder3 tool. We have started the implementation of this program code with the import of the “random” package of python. Firstly, we are displaying some important things in the console output screen using the print statement of python. The first print statement shows the title of the game. The second print statement shows the winning rules for this game. If the game is between rock and paper, the Paper will win. If the game is between rock and scissor, the rock will win, and if between paper and scissor, only the scissor will win.
print(" ****** ROCK PAPER SCISSOR ******\n")
print("Winning Rules: \n"
+"Rock vs paper-> Paper wins \n"
+ "Rock vs scissor-> Rock wins \n"
+"paper vs scissor-> Scissor wins \n")
So, while the situation and execution are going smoothly, the user will take the input. Firstly, the print statement is here to let the user know about the choices they have to make. The user will add a number and be saved to the variable “c.” After that, the while statement is used to check the condition, i.e., if “c” is other than 1,2 and 3, it will ask to add the valid input. If the user chooses 1, the user name will be “Rock.” If a user chooses 2, it will be “Paper,” and if chooses 3, it will be scissors. The print clause will display the user name.
print("Enter Your choice \n 1. Rock \n 2. paper \n 3. scissor \n")
c = int(input("User turn: ")) # c stands for user choice
while c > 3 or c< 1:
c = int(input("Enter valid input (1,2,3): "))
if c == 1:
name = 'Rock'
elif c == 2:
name = 'Paper'
else:
name = 'Scissor'
print("User choice is: " + name)
Here comes the computer’s turn. The random module has been utilized to use the “randint” function to choose a number between 1,2, or 3 and save it to the variable “cc.” If the computer’s choice “cc” is the same as the user’s choice “c,” it will ask the computer to choose again. So, the computer’s choice will be used within the “if-else” statement to check the added number and its respective name, i.e., rock, paper, or scissors. If the computer’s choice is 1, it will be “rock,” and if its choice is 2, it will be “paper.” The computer’s respective name choice will be saved to the variable “ccname.” In the last, if any other choice has been made by the computer, the name will be scissor this time. In the last, the print statement has been utilized to show the computer’s name on the console.
cc = random.randint(1, 3) #cc stands for computer choice
while cc == c:
cc = random.randint(1, 3)
if cc == 1:
ccname = 'Rock' #ccname stands for computer choice name
elif cc == 2:
ccname = 'paper'
else:
ccname = 'scissor'
print("Computer choice is: " + ccname)
The print statement has been used once again to show that the game will be between which user name and computer name. The “if-else” statement has been used here to check the situation while utilizing the OR operator. Within the “if” clause, if the game is between the rock and paper, the paper will be a winner, i.e., computer or user. The winner’s name will be saved to the variable “winner.” Within the “elif,” the condition will check that if the game is between the scissor and rock, only the “Rock” will be a winner. The “rock” name will be saved to the variable “winner.” In the else statement, if the game is between scissor and paper, the scissor will win, and the name of a winner would be saved to the variable “winner.”
if((c == 1 and cc == 2) or
(c == 2 and cc ==1 )):
print("Paper wins => ", end = "")
winner = "Paper"
elif((c == 1 and cc == 3) or
(c == 3 and cc == 1)):
print("Rock wins =>", end = "")
winner = "Rock"
else:
print("Scissor wins =>", end = "")
winner = "Scissor"
If the winner’s name matches the “user” name added by a user, it will print out that the user is a winner. Otherwise, it will print that the computer is a winner. The print statement asks the user if he/she wants to play once again by inputting “Y/N.” The input character entered by a user will be stored in the variable “sel.” The “if” statement will break the program if a user enters “n” or “N.” The last print statement will thank a user if a user doesn’t want to play more.
print("")
else:
print("")
print("Do you want to play again? (Y/N)")
sel = input()
if sel == 'n' or sel == 'N':
break
print("\nThankyou So much!")
After running the program, a display of win rules and choices is displayed.
The user has been asked to enter its choice, i.e., user-entered 1. The computer has chosen 2 and won the game. You can play the game once again by pressing “y.”
Conclusion:
This article has explained all the details about the rock paper scissor game in the python program. We have only utilized the “random” module, tried the “while” loop, if-else statements, and some print statements to achieve the whole game program. Therefore, we are extremely hopeful that this article will be a great help for every python beginner.