How to program the Rock Paper Scissor game in python?
Let’s create a simple command-line Rock Paper Scissor game using a random module only. The user gets the first chance to choose the Rock or Paper or Scissor options in this game. After that computer selects its choice randomly. The winner is decided as per the rules:
Winning Rules as follows :
Rock vs Paper -> Paper wins
Rock vs Scissor -> Rock wins
Paper vs Scissor -> Scissor wins.
If the computer’s choice and the user’s choice are the same, it will be a tie game and the user will get to choose another option. The game will be played until the loser or the winner is declared. Moreover, the program has a game menu that controls the play and the exit of the game.
code:
import random
print("\nWelcome to Rock Paper Scissor Game!")
print("""
***************************
* Game menu: *
* 1. Play *
* 2. Exit *
***************************\n""")
while True:
game_menu = input("Press 1 or 2:")
if game_menu == str(1):
def rockPaperScisor():
'''
Declaring a list of option
Making computer to choose any random option from the list and return
'''
global choice
choice = ['rock','paper','scissor']
computer_choice=random.choice(choice)
return computer_choice
computer_choice=rockPaperScisor()
value = True
while value == True:
user_choice = input("Enter your choice (Rock, Paper or Scissor): ").lower()
print()
# If user enters a wrong choice
if user_choice not in choice:
print("Please check your spelling and Try Again!\n")
value = True
# Condition for Tie
elif (user_choice == 'rock' and computer_choice == 'rock') or (user_choice == 'paper' and computer_choice == 'paper') or (user_choice == 'scissor' and computer_choice == 'scissor'):
print("Your choice: ", user_choice.title())
print("Computer's choice: ",computer_choice.title())
print("Tie, Try Again!\n")
value = True
# Condition for computer to win and user to lose
elif (user_choice == 'rock' and computer_choice == 'paper') or (user_choice == 'paper' and computer_choice == 'scissor') or (user_choice == 'scissor' and computer_choice == 'rock'):
print("Your choice: ", user_choice.title())
print("Computer's choice: ",computer_choice.title())
print("You Lost, Sorry!\n")
value = False
# Condition for computer to lose and user to win
elif (user_choice == 'paper' and computer_choice == 'rock') or (user_choice == 'scissor' and computer_choice == 'paper') or (user_choice == 'rock' and computer_choice == 'scissor'):
print("Your choice: ", user_choice.title())
print("Computer's choice: ",computer_choice.title())
print("You Won, Congratulations!\n")
value = False
# Option to exit game
elif game_menu == str(2):
print("Game Closed!")
break
# If user enters the wrong game option
else:
print("Wrong choice! Press 1 or 2\n")
Output:
Welcome to Rock Paper Scissor Game!
***************************
* Game menu: *
* 1. Play *
* 2. Exit *
***************************
Press 1 or 2:1
Enter your choice (Rock, Paper or Scissor): rock
Your choice: Rock
Computer's choice: Rock
Tie, Try Again!
Enter your choice (Rock, Paper or Scissor): paper
Your choice: Paper
Computer's choice: Rock
You Won, Congratulations!
Press 1 or 2:fds
Wrong choice! Press 1 or 2
Press 1 or 2:2
Game Closed!