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:
Tony Gaddis
Chapter:
Value Returning Functions And Modules
Exercise:
Programming Exercises
Question:12 | ISBN:9780132576376 | Edition: 2

Question

Rock, Paper, Scissors Game
Write a program that lets the user play the game of Rock, Paper, Scissors against the computer.
The program should work as follows.
1. When the program begins, a random number in the range of 1 through 3 is generated. If the number is 1, then the computer has chosen rock. If the number is 2, then the computer
has chosen paper. If the number is 3, then the computer has chosen scissors. (Don’t display the computer’s choice yet.)
2. The user enters his or her choice of “rock”, “paper”, or “scissors” at the keyboard.
3. The computer’s choice is displayed.
4. A winner is selected according to the following rules:

  • If one player chooses rock and the other player chooses scissors, then rock wins.(The rock smashes the scissors.)
  • If one player chooses scissors and the other player chooses paper, then scissors wins. (Scissors cuts paper.)
  • If one player chooses paper and the other player chooses rock, then paper wins.(Paper wraps rock.)
  • If both players make the same choice, the game must be played again to determine the winner.
TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

Rock, Paper, Scissors Game Program code:

#Header section of random
import random
# When the program begins, a random number in the range of 1 through 3 
#is generated. If the number is 1, then the computer has chosen rock. 
#If the number is 2, then the computer has chosen paper. 
#If the number is 3, then the computer has chosen scissors. 
#The user enters his or her choice of “rock”, “paper”, or 
#“scissors” at the keyboard.  The computer’s choice is displayed.
def findWinner(playerChoice, computerChoice):
    if playerChoice == computerChoice:
        return "It's a tie!"

    if (playerChoice == "rock" and computerChoice == "scissors") or \
       (playerChoice == "scissors" and computerChoice == "paper") or \
       (playerChoice == "paper" and computerChoice == "rock"):
        return "You(player) win!"
    else:
        return "Computer wins!"

def play_game():
    choices = ["rock", "paper", "scissors"]
    computerChoice = random.choice(choices)

    while True:
        playerChoice = input("Enter your choice (rock/paper/scissors): ").lower()

        if playerChoice in choices:
            print("Computer's choice:", computerChoice)
            print(findWinner(playerChoice, computerChoice))
            break
        else:
            print("Invalid choice. Please enter rock, paper, or scissors.")

play_game()

 

Executed Output 1:

   
Enter your choice (rock/paper/scissors): 
rock
Computer's choice: scissors
You(player) win!

Executed Output 2:

   
Enter your choice (rock/paper/scissors): 
scissors
Computer's choice: paper
You(player) win!

 

0 0

Discussions

Post the discussion to improve the above solution.