Skip to content

Instantly share code, notes, and snippets.

@pranshuj73
Last active October 14, 2020 20:17
Show Gist options
  • Save pranshuj73/bcbec00f7ebbb2d8e2e999ec0ca984b2 to your computer and use it in GitHub Desktop.
Save pranshuj73/bcbec00f7ebbb2d8e2e999ec0ca984b2 to your computer and use it in GitHub Desktop.
Instructions for making rock-paper-scissors game in python

Rock-Paper_Scissors in Python

Algorithm:

  • Collect user input and check if user's move is rock, paper or scissors and simultaneously use random library to generate a random computer move.
  • Use if-elif-else or if-else statements to compare the user's move and computer's move
    • if user's move is superior to computer's move (eg: rock > scissors and similarly paper > rock), print that user won
    • else print user lost

Things to learn to make the game:

  • if-elif-else statements and logical operators
  • random library of python

Also don't hesitate to google things you're not very sure about/ don't know about.

# sample rock-paper-scissors game made with python
# author: volt9801
import random # importing random library for choosing random computer move
# list of possible valid moves which can be entered by user
moves = [('r',"rock", "1"), ('p',"paper", "2"), ('s',"scissors", "3")]
# dictionary of moves and their superior counterparts
win_moves = {"rock": "paper", "paper": "scissors", "scissors": "rock"}
keep_playing = True
while keep_playing:
user_move = input("Choose a move [r/rock/1, p/paper/2, s/scissors/3]: ").lower()
computer_move = random.choice(["rock", "paper", "scissors"])
if user_move in moves[0]: # if user chooses rock
print("Computer chose", computer_move)
if win_moves[computer_move] == "rock":
print("You won!")
elif computer_move == "rock":
print("It's a tie!")
else:
print("You lost :[")
elif user_move in moves[1]: # if user chooses paper
print("Computer chose", computer_move)
if win_moves[computer_move] == "paper":
print("You won!")
elif computer_move == "paper":
print("It's a tie!")
else:
print("You lost :[")
elif user_move in moves[2]: # if user chooses scissors
print("Computer chose", computer_move)
if win_moves[computer_move] == "scissors":
print("You won!")
elif computer_move == "scissors":
print("It's a tie!")
else:
print("You lost :[")
else: # if the user chooses an invalid move
print("Invalid move! Please try again")
play_again = input("Would you like to continue playing? [y/n] ").lower()
if play_again == "n" or play_again == "no":
keep_playing = False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment