How to make the rock paper scissor program take less lines in if/else statements

Question:

Repl link:

p1p = 0
p2p = 0

while True:
  player1 = input("R,P or S?")
  player2 = input("R,P or S?")
  if player1 == "R" and player2 == "S":
    print("Player 1 wins!")
    p1p += 1
    if p1p == 3:
      print("Victory for player 1!")
      exit()
    elif p2p == 3:
      print("Victory for player 2")
      exit()
  elif player1 == "R" and player2 == "P":
    print("Player 2 wins!")
    p2p += 1
    if p1p == 3:
      print("Victory for player 1!")
      exit()
    elif p2p == 3:
      print("Victory for player 2")
      exit()
  elif player1 == "S" and player2 == "R":
    print("Player 2 wins!")
    p2p += 1
    if p1p == 3:
      print("Victory for player 1!")
      exit()
    elif p2p == 3:
      print("Victory for player 2")
      exit()
  elif player1 == "P" and player2 == "R":
    print("Player 1 wins!")
    p1p += 1
    if p1p == 3:
      print("Victory for player 1!")
      exit()
    elif p2p == 3:
      print("Victory for player 2")
      exit()
  elif player1 == "P" and player2 == "S":
    print("Player 2 wins!")
    p2p += 1
    if p1p == 3:
      print("Victory for player 1!")
      exit()
    elif p2p == 3:
      print("Victory for player 2")
      exit()
  elif player1 == "S" and player2 == "P":
    print("Player 1 wins!")
    p1p += 1
    if p1p == 3:
      print("Victory for player 1!")
      exit()
    elif p2p == 3:
      print("Victory for player 2")
      exit()
  elif player1 == player2:
    print("Its a tie!")

Welcome to the forums @AayushSalil!

I don’t understand what you mean. Could you please elaborate?

1 Like

think they mean that they want to make the program shorter

1 Like

Hey @AayushSalil, welcome to the community!

Here is some shorter code, with more features too:

p1p = 0
p2p = 0


def determine_player1_win(condition: bool) -> None:
    """Prints that player 1 wins and increments `p1p` if `condition` is true"""
    
    if condition:
        print("Player 1 wins!")
        global p1p
        p1p += 1
    else:
        print("Player 2 wins!")
        global p2p
        p2p += 1


while True:
    if p1p == 3:
        print("Victory for player 1!")
        break
    elif p2p == 3:
        print("Victory for player 2!")
        break
    
    player1 = input("R(ock), P(aper) or S(cissors)? ").upper()
    player2 = input("R(ock), P(aper) or S(cissors)? ").upper()

    if not player1:
        print("Player 1 didn't input anything...")
        continue
    elif not player2:
        print("Player 2 didn't input anything...")
        continue
    else:
        player1 = player1[0]
        player2 = player2[0]
    
    if player1 == player2:
        print("It's a tie!")
        continue
    elif player1 == "R":
        determine_player1_win(player2 == "S")
    elif player1 == "P":
        determine_player1_win(player2 == "R")
    elif player1 == "S":
        determine_player1_win(player2 == "P")

You can see that I broke the check and incrementing of the points into its own function, determine_player1_win. You’ll also notice that instead of writing the victory check over and over, I just put it at the top of the while True loop.

        player1 = player1[0]
        player2 = player2[0]

Here you can see I’m using string slicing to get the first letter the user input. That’s pretty much everything I did.

image

My implementation:

p1p = 0
p2p = 0

def get_input(prompt="R,P or S?", retry="Invalid input"):
  while True:
    inp = input(prompt).upper()
    if inp in ("R","P","S"):
      return inp
    print(retry)

loses_to = {  # value loses to key
  "R": "S",
  "P": "R",
  "S": "P"
}

while True:
  player1 = get_input()
  player2 = get_input()
  if player1 == player2:
    print("Its a tie!")
  elif player2 == loses_to[player1]:
    print("Player 1 wins!")
    p1p += 1
  else:
    print("Player 2 wins!")
    p2p += 1
  if p1p == 3:
    print("Victory for player 1!")
    break
  elif p2p == 3:
    print("Victory for player 2!")
    break
1 Like
from os import system as sys, name as user
from random import randint as r, shuffle as s

def clear():  #clear console function
  if user == 'nt':
    _ = sys("cls")
  else:
    _ = sys("clear")

choices = ["rock", "paper", "scissors"]
randList = [0, 1, 2]
beatinchoices = ["paper", "scissors", "rock"]
CUC = 3
CCC = 3
wincount = [
  0, 0, 0
] 
while True:
  clear()
  while not CUC in [0, 1, 2] and not CCC in [0, 1, 2]:
    #make sure user inputs the right thing
    try:
     CCC = int(
        input(
          f"WIN COUNT:\nP1: {wincount[0]} times\nP2: {wincount[1]} times\n\n\n1. Rock\n2. Paper\n3. Scissors\nEnter the number representing which gesture you want to choose: "
        )) - 1
      CUC = int(
        input(
          f"WIN COUNT:\nP1: {wincount[0]} times\nP2: {wincount[1]} times\n\n\n1. Rock\n2. Paper\n3. Scissors\nEnter the number representing which gesture you want to choose: "
        )) - 1
    except ValueError:
      clear()
    finally:
      clear()
  s(randList)
  _ = input(
    f'P1 chose {choices[CCC]}\nP2 chose {choices[CUC]}\n{"P1 won!" if choices[CCC] == beatinchoices[CUC] else ("P2 won!" if choices[CUC] == beatinchoices[CCC] else "It is a tie!")}\n'
  )
  wincount[0 if choices[CCC] == beatinchoices[CUC] else 1 if choices[CUC] ==
           beatinchoices[CCC] else 2] += 1

Here is mine, looks weird cuz it is directly copied from here lol