SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Y Daniel Lang
Chapter:
.selections
Exercise:
Programming Excercises
Question:17 | ISBN:978013274719 | Edition: 6

Question

(Game: scissor, rock, paper) Write a program that plays the popular scissor-rockpaper game. (A scissor can cut a paper, a rock can knock a scissor, and a paper can wrap a rock.) The program randomly generates a number 0, 1, or 2 representing scissor, rock, and paper. The program prompts the user to enter a number 0, 1, or 2 and displays a message indicating whether the user or the computer wins, loses, or draws. Here are sample runs:

scissor (0), rock (1), paper (2): 1
The computer is scissor. You are rock. You won.

scissor (0), rock (1), paper (2): 2
The computer is paper. You are paper too. It is a draw.
 

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

#Import random module
import random
computer = random.randint(0,3)
#Prompt the user to enter a number 0, 1, or 2
print("scissor (0), rock (1), paper (2): ")
user = int(input())
print("The computer is ",end=" ")
switcher1={ 0: "scissor.",
		   1: "rock.",
		   2: "paper."
		}
print(switcher1.get(computer),end="")

print(" You are ",end=" ")
switcher2={ 0:"scissor",
			1: "rock",
			2:"paper "
		}
print(switcher2.get(user),end="")

#Display result
if (computer == user):
    print(" too. It is a draw");
elif(user == 0 and computer == 2) or(user == 1 and computer == 0) or (user == 2 and computer == 1):
    print(". You won.")
else:
    print(". You lose.")
		

Output:

scissor (0), rock (1), paper (2): 
2
The computer is  rock. You are  paper . You won.

0 0

Discussions

Post the discussion to improve the above solution.