Explore a world of knowledge and get your questions answered on IDNLearn.com. Join our community to access reliable and comprehensive responses to your questions from experienced professionals.

ROCK-PAPER-SCISSORS Write a program that plays the popular game of rock-paper-scissors. (A rock breaks scissors, paper covers rock, and scissors cut paper). The program has two players and prompts the users to enter rock, paper or scissors. Your program does not have to produce any color. The color is there just to show you what your program should be inputting and outputting. The writing in blue represents output and writing in red represents input. Here are some sample runs:

Sagot :

Answer:

In Python

print("Instruction: 1. Rock, 2. Paper or 3. Scissors: ")

p1 = int(input("Player 1: "))

p2 = int(input("Player 2: "))

if p1 == p2:

   print("Tie")

else:

   if p1 == 1: # Rock

       if p2 == 2: #Paper

           print("P2 wins! Paper covers Rock")

       else: #Scissors

           print("P1 wins! Rock smashes Scissors")

   elif p1 == 2: #Paper

       if p2 == 1: #Rock

           print("P1 wins! Paper covers Rock")

       else: #Scissors

           print("P2 wins! Scissors cuts Paper")

   elif p1 == 3: #Scissors

       if p2 == 1: #Rock

           print("P2 wins! Rock smashes Scissors")

       else: #Paper

           print("P1 wins! Scissors cuts Paper")

Explanation:

This prints the game instruction

print("Instruction: 1. Rock, 2. Paper or 3. Scissors: ")

The next two lines get input from the user

p1 = int(input("Player 1: "))

p2 = int(input("Player 2: "))

If both input are the same, then there is a tie

if p1 == p2:

   print("Tie")

If otherwise

else:

The following is executed if P1 selects rock

   if p1 == 1: # Rock

If P2 selects paper, then P2 wins

       if p2 == 2: #Paper

           print("P2 wins! Paper covers Rock")

If P2 selects scissors, then P1 wins

       else: #Scissors

           print("P1 wins! Rock smashes Scissors")

The following is executed if P1 selects paper

   elif p1 == 2: #Paper

If P2 selects rock, then P1 wins

       if p2 == 1: #Rock

           print("P1 wins! Paper covers Rock")

If P2 selects scissors, then P2 wins

       else: #Scissors

           print("P2 wins! Scissors cuts Paper")

The following is executed if P1 selects scissors

   elif p1 == 3: #Scissors

If P2 selects rock, then P2 wins

       if p2 == 1: #Rock

           print("P2 wins! Rock smashes Scissors")

If P2 selects paper, then P1 wins

       else: #Paper

           print("P1 wins! Scissors cuts Paper")