Create a ROCK- PAPER-SCISSORS Game with Python
Seun
Posted on September 10, 2023
In this post, we'll delve into crafting a simplified version of the rock-paper-scissors game. But first, let's familiarize ourselves with the core concept behind rock-paper-scissors.
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!).
With a clear grasp of the game and its rules, let's deconstruct the gameplay mechanics and translate them into code. Additionally our code will be broken down with the below steps
- import
random
modules; this gives us random functionality - Our game involves two participants: the user and the computer.
- We define a list that contains the computer's possible moves: rock, paper, and scissors.
- The computer randomly picks one of the choices in the list of container
- A users choice is captured and stored in a variable
- Conditional
if
statements are utilized to compare the computer's move with the user's choice and establish the outcome: a win, a loss, or a draw.
#Import the random module for random functionality
import random
# Printing a header and welcome message
print('*'*50)
print('Welcome to Rock, Paper & Scissors Game')
print('*'*50)
#We define a list that contains the computer's possible moves: rock, paper, and scissors.
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 captured and 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)
Well done on creating your first rock-paper-scissors game! For an enhanced version, check out my GitHub
Posted on September 10, 2023
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
November 30, 2024
November 30, 2024