Easy Steps To Create Rock-Paper-Scissors Game with Python

Easy Steps To Create Rock-Paper-Scissors Game with Python

It's fun and easy to try out so if you just started with Python programming, this article will still help.

import random

def the_game():
  player_choice = input("Enter a choice (rock, paper or scissors): ")
  options = ["rock", "paper", "scissors"]
  computer_choice = random.choice(options)
  response = {"player": player_choice, "computer": computer_choice}
  return response

def check_winner(player, computer):
  print(f"You chose {player}, computer chose {computer}")
  if player == computer:
    return "A tie!"
  elif player == "rock":
    if computer == "scissors":
      return "Rock smashes scissors! You win!"
    else:
      return "Paper covers rock. You lose."
  elif player == "paper":
    if computer == "paper":
      return "Paper covers rock. You win!"
    else:
      return "Scissors cuts paper. You lose."
  elif player == "scissors":
    if computer == "paper":
      return "Scissors cuts paper. You win!"
    else:
      return "Rock smashes scissors! You lose."

response = the_game()
score = check_winner(response["player"], response["computer"])
print(score)

Check the next post for the breakdown of each code block. Trust me, it's easy and fun to try out.