Skip to main content
2 of 5
edited body
LMays
  • 155
  • 5

How can i fix my constant nesting as a beginner?

import random
#1 = Rock, 2 = Paper, 3 = Scissors
random_number = random.randint(1,3)
try:
  input = int(input("1 for rock, 2 for paper, 3 for scissors: "))
except ValueError:
  print("You didn't enter a number. You lose.")

if input == random_number:
  if input == 1:
    input = "Rock"
  elif input == 2:
    input = "Paper"
  else:
    input = "Scissors"
  if random_number == 1:
    random_number = "Rock"
  elif random_number == 2:
    random_number = "Paper"
  else:
    random_number = "Scissors"
  print(f"You chose {input} and the computer chose {random_number}. You Tie.")

elif (input==1 and random_number==2) or (input==2 and random_number==3) or (input==3 and random_number==1):
  if input == 1:
    input = "Rock"
  elif input == 2:
    input = "Paper"
  else:
    input = "Scissors"
  if random_number == 1:
    random_number = "Rock"
  elif random_number == 2:
    random_number = "Paper"
  else:
    random_number = "Scissors"
  print(f"You chose {input} and the computer chose {random_number}. You Lose.")

elif (input==1 and random_number==3) or (input==2 and random_number==1) or (input==3 and random_number==2):
  if input == 1:
    input = "Rock"
  elif input == 2:
    input = "Paper"
  else:
    input = "Scissors"
  if random_number == 1:

        random_number = "Rock"
      elif random_number == 2:
        random_number = "Paper"
      else:
        random_number = "Scissors"
      print(f"You chose {input} and the computer chose {random_number}. You Win.") 
      
    else:
      if type(input) is int:
        print("You chose an invalid number. You Lose.")

Blockquote This code should play rock paper scissor against the user. >I was wonder how exactly should I handle my constant nesting.

LMays
  • 155
  • 5