Create a ROCK- PAPER-SCISSORS Game with Python
This post will explore how to create a minimal rock-paper-scissor. However, before we make the game, we should understand what a rock-paper-scissors game is about.
Rock, paper, scissors is a classic game of chance for two people. You and a partner shake your fists three times and then make gestures at random to show a rock, paper, or scissors. Rock beats scissors, scissors beat paper, and paper beats rock (it wraps the rock!).
Now that we understand the game and how to play it. We will break down the logic to code the game.
- import random modules; this gives us random functionality
- The game needs two players, a user and a computer
- The computer has three choices stored in a list container
- The computer randomly picks one of the choices in the list of container
- A users choice is stored in a variable
- An IF logic is used to check for computers choice and users the choice to determine a Winner or a Tie
import random
# Printing a header and welcome message
print('*'*50)
print('Welcome to Rock, Paper & Scissors Game')
print('*'*50)
#The computer has three choices stored in a list container
computer_choice = ['SCISSORS', 'ROCK', 'PAPER']
#The computer randomly picks one of the choices in the list of container
computer_choices = random.choice(computer_choice)
#A users choice is stored in a variable
user_choice = str(input('Do you want - Rock, Paper, or Scissors?: '))
user_choice = user_choice.upper()
if computer_choices == user_choice:
print("It's a TIE. You have chosen", user_choice, "and Computer has chosen,", computer_choices)
print()
elif user_choice == 'ROCK' and computer_choices == 'SCISSORS':
print(" YOU WIN !!! | You Chose", user_choice, 'and Computer has chosen', computer_choices)
elif user_choice == 'PAPER' and computer_choices == 'ROCK':
print(" YOU WIN !!! | You Chose", user_choice, 'and Computer has chosen', computer_choices)
elif user_choice == 'SCISSORS' and computer_choices == 'PAPER':
print(" YOU WIN !!! | You Chose", user_choice, 'and Computer has chosen', computer_choices)
else:
print("Sorry You lose !!! | You Chose", user_choice, 'and Computer has chosen', computer_choices)
Congratulations, you have just built your first rock-paper-scissors game; here is a link to my GitHub for the updated version of this game.
Subscribe to my newsletter
Read articles from Seun directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by