Wednesday, January 15, 2020

Rock - Paper - Scissors in Python 3

# An implementation of Rock-Paper-Scissors in Python

import random

CChoice, PChoice, CScore, PScore = 0, 0, 0, 0
Set = ["Rock", "Paper", "Scissors"]
GameNo = 1
PChoice, pchoice = -1, 0

while GameNo <= 20:
  CChoice = random.randint(1, 3)
  s = "\nGame " + str(GameNo) + ": " + "Enter (R)ock, (P)aper, (S)cissors OR (E)xit: "
  pChoice = input(s).upper()
  if pChoice == 'R':
    PChoice = 1
  elif pChoice == 'P':
    PChoice = 2
  elif pChoice == 'S':
    PChoice = 3
  elif pChoice == 'E':
    PChoice = 0
  else:
    print("Invalid choice")
    continue
  
  if pChoice == 'E':
    break

  print("Computer chose", end = ' ');  print(Set[CChoice-1], end = ' ')
  print("You chose", end = ' ');       print(Set[PChoice-1])

  if CChoice == 1:
    if PChoice == 1:
      print("Both chose Rock - DRAW")
    elif PChoice == 2:
      print("Paper covers Rock", end = '')
      print(" - YOU WIN")
      PScore += 1
    elif PChoice == 3:
      print("Rock smashes Scissors", end = '')
      print(" - COMPUTER WINS")
      CScore += 1
  elif CChoice == 2:
    if PChoice == 1:
      print("Paper covers Rock ", end = '')
      print(" - COMPUTER WINS")
      CScore += 1
    elif PChoice == 2:
      print("Both chose Paper, DRAW")
    elif PChoice == 3:
      print("Scissors cut Paper", end = '')
      print(" - YOU WIN")
      PScore += 1
  elif CChoice == 3:
    if PChoice == 1:
      print("Rock smashes Scissors", end = '')
      print(" - YOU WIN")
      PScore += 1
    elif PChoice == 2:
      print("Scissors cut Paper", end = '')
      print(" - COMPUTER WINS")
      CScore += 1
    elif PChoice == 3:
      print("Both chose Scissors, DRAW")
  print("Computer Score = ", CScore, " Your Score = ", PScore)

  GameNo += 1

No comments:

Post a Comment