Question

In: Computer Science

Subjects: Write a Python program that allows users to play the popular rock-paper-scissor game against the...

Subjects: Write a Python program that allows users to play the popular rock-paper-scissor game against the computer multiple times. This program assesses your knowledge of decision structures, repetition structures, and functions.

Requirements:

1.Define a function named as play_game. This function receives two parameters representing the player’s and computer’s choices, and it returns an integer representing the game result from the player’s perspective. Specifically, it returns 1 if the player wins and 0 if the player does not win. Hint: you can reuse most of the code of team activity 2 as the body of this function.

2.Define the main function. In the main function, write code to let users play the game multiple rounds against the computer.

    1)Option 1: use a for-loop to let the player play this game 3 times. For each round of the game, display a message indicating whether the player wins, loses, or draws. After the player has played 3 times, display a message indicating how many times the player has won.

    2)Option 2: use a while loop to let the player play this game multiple times and the loop ends when the player wins 3 times. For each round of the game, display a message indicating whether the player wins, loses, or draws. After the player has won 3 times, display a message informing the player that the player has won 3 out of how many times played.

    3)For either option, within each round of the game:

        a. Let the computer randomly choose scissors, rock, and paper.

        b.Prompt the player to make the player’s choice.

        c.Call the play_game function to play the game once.

3. Call the main function and test your code.

Solutions

Expert Solution

Here our task is to implement a ROCK-PAPER-SCISSOR game which can be played agains computer

Things to remeber before coding

  • We have to define a function which recives both users and computers choice and return apropriate value
  • In main choose a loop that runs 3 times (i have choosed option one [for loop])
  • Take input from user and choose computers choice by generating a random number
  • If the round is not draw call rthe play_game function and recives value from it.Print apropraite outputs

Now let's see How to code this in python

CODE

(Please read all comments for the better understanding of the program)

Sample Run

I am also attching the text version of the code in case you need to copy paste

import random #importing random package

def play_game(user,computer): #function to check whether user or computer wins
if (user == 1): #nested if cases to check all possibilities
if(computer == 2):
return 0
elif(computer == 3):
return 1
elif (user == 2):
if(computer == 1):
return 1
elif(computer == 3):
return 0
elif (user == 3):
if(computer == 2):
return 1
elif(computer == 0):
return 0


  
  
if __name__=="__main__": #main function starts here
  
win=0 #varaible to store counr for win
  
lst=["You LOSE","You WON","Draw"] #A list used to store results
for i in range(3): #option 1
  
user=int(input("paper(1)__Scissor(2)__Rock(3) :")) #geting input from user and stores to user
computer=random.randint(1,3) #generating random varable and stores to vaiable computer
print(computer) #printing the computers choice
if (user != computer): #checking for draws
y=play_game(user,computer) #calling play_game functions
print(lst[y])   
  
win=win+y #count increasing if user wins
else:
print(lst[2])
  
print("You won",win,"times")
  
  


  


Related Solutions

Rock, Paper, Scissors Game Write a Python program rps.py that lets the user play the game...
Rock, Paper, Scissors Game Write a Python program rps.py that lets the user play the game of Rock, Paper, Scissors against the computer. The program should work as follows: You can set these constant global variables at the top outside of your main function definition: COMPUTER_WINS = 1 PLAYER_WINS = 2 TIE = 0 INVALID = 3 ROCK = 1 PAPER = 2 SCISSORS = 3 For this program 1 represents rock, 2 represents paper, and 3 represents scissors. In...
Please use Python 21. Rock, Paper, Scissors Game Write a program that let's the user play...
Please use Python 21. Rock, Paper, Scissors Game Write a program that let's 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...
write a python script for rock scissors paper game
write a python script for rock scissors paper game
Write a program using Python that allows the user to play a guessing game (please provide...
Write a program using Python that allows the user to play a guessing game (please provide a picture of code so it is easier to read). The game will choose a "secret number", a positive integer less than 10000. The user has 10 tries to guess the number. Requirements: Normally, we would have the program select a random number as the "secret number". However, for the purpose of testing your program (as well as grading it), simply use an assignment...
Write a Java program that plays the game Rock, Paper, Scissors. The program should generate a...
Write a Java program that plays the game Rock, Paper, Scissors. The program should generate a random choice (Rock, Paper or Scissors) then ask the user to choose Rock, Paper or Scissors. After that the program will display its choice and a message showing if the player won, lost or tied. Next, the program should prompt the user to play again or not. Once the player selects to stop playing the game, the program should print the number of wins,...
Problem Description: You have to play the rock-paper-scissors game against the computer for 100 times. You...
Problem Description: You have to play the rock-paper-scissors game against the computer for 100 times. You receive the following rewards each time you play the game:  You get $5 if you win  You get $2 if there is a tie  You get $-1 if you lose The computer randomly chooses rock, paper, or scissors in each game. Rather than deciding what to play (rock, paper or scissors) for each individual game, you decide to use the following...
I am using C++ Write a program that allows two players to play a game of...
I am using C++ Write a program that allows two players to play a game of tic-tac-toe. Use a two-dimensional char array with three rows and three columns as the game board. Each element of the array should be initialized with an asterisk (*). The program should run a loop that does the following: Displays the contents of the board array. Allows player 1 to select a location on the board for an X. The program should ask the user...
Write a program that allows two players to play a game of tic-tac-toe. Use a two-dimensional...
Write a program that allows two players to play a game of tic-tac-toe. Use a two-dimensional char array with three rows and three columns as the game board. Each element of the array should be initialized with an asterisk (*). The program should run a loop that does the following: Displays the contents of the board array. Allows player 1 to select a location on the board for an X. The program should ask the user to enter the row...
One file java program that will simulate a game of Rock, Paper, Scissors. One of the...
One file java program that will simulate a game of Rock, Paper, Scissors. One of the two players will be the computer. The program will start by asking how many winning rounds are needed to win the game. Each round will consist of you asking the user to pick between rock, paper, and scissors. Internally you will get the computers choice by using a random number generator. Rock beats Scissors, Paper beats Rock, and Scissors beats Paper. You will report...
Game Description: The popular rock-paper-scissors game is usually played between two people in which each player...
Game Description: The popular rock-paper-scissors game is usually played between two people in which each player simultaneously chooses either a rock or a paper or scissors (usually with an outstretched hand). The rule of the game is simple: rock crushes scissors, scissors cut paper, and paper wraps rock. If both the players choose the same object, then it ends in a tie. Problem Description: You have to play the rock-paper-scissors game against the computer for 100 times. You receive the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT