In this post, we will write a simple Python program to implement the Rock, Paper, Scissors game with a detailed explanation.
Rules for Rock, Paper, Scissors game
Rock
is won overscissors
Scissor
is won overPaper
Paper
is won overRock
Algorithms for Rock, Paper, Scissors
Step-1:import random
module.
Step-2: create a list [stone,paper,scissor]
Step-3: take user input 0 --> stone, 1 --> paper, 2 --> scissor
(user choice).
Step-3: using random.randint(0,2)
generate random number from 0 to 2 (this is a computer choice).
Step-4: print user_choice
and computer_choice
.
Step-5: if user_choice == 0 and computer_choice == 2
then user will win (0 stand for stone and 2 stand for scissors).
Step-6: elif user_choice == 2 and computer_choice == 0
then computer will win (opposite of step-5).
Step-7: elif computer_choice>user_choice
then computer win because now only two options are left 0 or 1 (0 for stone or 1 for paper). if computer_choice>user_choice
means computer choice is 1 which is equal to paper and user choice is 0 which is equal to stone (paper won over a stone).
Step-8: elif user_choice>computer_choice
then user wins (opposite of step-7).
Step-9: otherwise the game is a draw.
this is a very exciting python mini project for beginners. So let’s open your IDEs and start writing code for the stone, paper, scissors game.
Bur before writing a program you should know about:
- random module
- python list
- if-elif-else
- python operators.
Source code
import random
stone = '''
---' ____)
(_____)
(_____)
(____)
---.__(___)
'''
paper = '''
_______
---' ____)____
______)
_______)
_______)
---.__________)
'''
sizzer = '''
---' ____)____
______)
__________)
(____)
---.__(___)
'''
image_choice = [stone,paper,sizzer]
print('stone paper sizerer game choose 0-stone, 1-paper, 2-sizzer')
user_choice = int(input('Enter your choise : ')) #user choice
print('Your choice:\n')
print(image_choice[user_choice])
computer_choice = random.randint(0,2) #computer choice
print('Computer choice:\n')
print(image_choice[computer_choice])
if user_choice >2 or user_choice<0:
print('You lose !! (Because choice wrong input )')
elif user_choice == 0 and computer_choice == 2:
print('you win !!')
elif computer_choice == 0 and user_choice == 2:
print('You Lose !!')
elif computer_choice>user_choice:
print('you Lose !')
elif user_choice>computer_choice:
print('you win !!')
elif user_choice == computer_choice:
print('It\'s Draw !!!')
Output
stone paper sizerer game choose 0-stone, 1-paper, 2-sizzer
Enter your choise : 2
Your choice:
---' ____)____
______)
__________)
(____)
---.__(___)
Computer choice:
_______
---' ____)____
______)
_______)
_______)
---.__________)
you win !!
Hope this post adds some value to your life – thank you.