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

Using Java, write a program that allows the user to play the Rock-Paper-Scissors game against the...
Using Java, write a program that allows the user to play the Rock-Paper-Scissors game against the computer through a user interface. The user will choose to throw Rock, Paper or Scissors and the computer will randomly select between the two. In the game, Rock beats Scissors, Scissors beats Paper, and Paper beats Rock. The program should then reveal the computer's choice and print a statement indicating if the user won, the computer won, or if it was a tie. Allow...
Java) Write a program to score the rock-paper-scissor game. Each of two users types in either...
Java) Write a program to score the rock-paper-scissor game. Each of two users types in either R, P, S or Q to quit. The program then announces the winner as well as the basis for determining the winner: Paper covers rock, Rock breaks scissors, Scissors cut paper, or a Tie game. Allow the user to use lowercase as well as uppercase letters. Your program should loop until the user types a 'Q' to quit. In the sample code, the game...
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 C++ program to score the paper-rock-scissor game. Each of two players (player1 and player2)...
Write a C++ program to score the paper-rock-scissor game. Each of two players (player1 and player2) input a character which could be either ‘P’, ‘R’, or ‘S’ (in uppercase or lowercase). For any other input character should display a message “Invalid input”. The program then announces who is the winner as well as the basis for determining the winner which could be one of the following: “Paper covers rock”, “Rock breaks scissors”, “Scissors cut paper”, or “Nobody wins”. (Use switch...
Write a C++ program to score the paper-rock-scissor game. Each of two players (player1 and player2)...
Write a C++ program to score the paper-rock-scissor game. Each of two players (player1 and player2) input a character which could be either ‘P’, ‘R’, or ‘S’ (in uppercase or lowercase). For any other input character should display a message “Invalid input”. The program then announces who is the winner as well as the basis for determining the winner which could be one of the following: “Paper covers rock”, “Rock breaks scissors”, “Scissors cut paper”, or “Nobody wins”. (Use switch...
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...
JAVA PROGRAM, Create the game "Rock, Scissor, Paper": Make the AI pick randomly. You need to...
JAVA PROGRAM, Create the game "Rock, Scissor, Paper": Make the AI pick randomly. You need to look up Random numbers in Java. First ask the user what language they want to play in. Choice 1 is English, but choice 2 can be whatever real language you want. Your first loop is making sure they enter good input. If there is a tie you don't give them the option to play again. They have to. Outer loop runs until they say...
Create a Java program that allows two players to play Rock, Paper, Scissors. Player 1 will...
Create a Java program that allows two players to play Rock, Paper, Scissors. Player 1 will enter an integer to determine whether they use rock, paper or scissors. Player 2 will also enter an integer to determine whether they use rock, paper or scissors. Use named constants for rock, paper and scissors and set their values as shown below. Use if-else statements to determine the results of the game. Use the named constants to compare with the player 1 and...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT