Question

In: Computer Science

write a python script for rock scissors paper game

write a python script for rock scissors paper game

Solutions

Expert Solution

The following is the python script for the rock, scissors, and paper game. Making use of nested if-else block will do for this game. Proper explanation is given in comments.

#import randint from random module
from random import randint

#This is the list of shapes during play
shapes=["rock","paper","scissors"]

#Here we assign a random from the shapes to computer
computer=shapes[randint(0,2)]

#Initially set player to False
flag=True

#make a count of all rounds, which include tie,player_wins,computer_wins
count=0
tie=0
player_wins=0
computer_wins=0

while flag: #This loop continues till player enters -1
    player=input("Rock, Paper, Scissors? ").lower()
    count+=1 #take count increment by 1
    if player==computer: #if player and computer are equal, then its a tie
        print("It's a Tie!!!")
        tie+=1 #increment tie by 1
    elif player=="rock":
        if computer=="paper":
            print("You lose!! -> "+computer+" wins")
            computer_wins+=1 #increment computer_wins by 1
        else:
            print("You win!! -> "+player+" wins")
            player_wins+=1 #increment player_wins by 1
    #same rules respectively for other shapes
    elif player=="paper":
        if computer=="scissors":
            print("You lose!! -> "+computer+" wins")
            computer_wins+=1
        else:
            print("You win!! -> "+player+" wins")
            player_wins+=1
    elif player=="scissors":
        if computer=="rock":
            print("You lose!! -> "+computer+" wins")
            computer_wins+=1
        else:
            print("You win!! -> "+player+" wins")
            player_wins+=1
    elif player=="-1": #if player enters -1, then stop the game
        count-=1     #donot count this attempt
        flag=False   #set flag to False
    else: #else, this is an invalid option
        count-=1  #donot count this attempt
        print("Invalid shape!! Please try again")
    computer=shapes[randint(0,2)] #assign computer some random shape
   
#print the game result 
print("Game Over!!!")
print("Number of games played "+str(count))
print("Number of player wins "+str(player_wins))
print("Number of computer wins "+str(computer_wins))
print("Number of Tie "+str(tie))

I am also attaching the output and code screenshot for your reference. It will help you to correct if any indentation errors.

Output and code screenshot:

#Please don't forget to upvote if you find the solution helpful. Feel free to ask doubts if any, in the comments section. Thank you.


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 Java class that determines the winner of a rock, paper scissors game. Assume the...
Write a Java class that determines the winner of a rock, paper scissors game. Assume the input from the user is always valid (so no need to check), that is it contains either one of `R`, `P`, or `S` as a single character, or has matching parenthesis, like, `(S&P)` or `((R&P)&S)`, and the `&` character. So for example, the user inputs `(P&R)` and the program will output `P` since paper beats rock. Or if the user inputs `((S&R)&(S&S))` the output...
Please write a scheme code for the rock paper scissors game between a player and the...
Please write a scheme code for the rock paper scissors game between a player and the computer (AI). In scheme programming language please.
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,...
Solve the scissors, paper, rock game. This game is well known in many parts of the...
Solve the scissors, paper, rock game. This game is well known in many parts of the world. Two players simultaneously present a hand in one of three positions: an open hand (paper), a closed fist (rock), or two open fingers (scissors). The payoff is 1 unit according to the rule “Paper covers rock, rock breaks scissors, and scissors cut paper.” If both players present the same form, the payoff is 0. Set up the payoff matrix for the game and...
In the game Rock Paper Scissors, two players simultaneously choose one of three options: rock, paper,...
In the game Rock Paper Scissors, two players simultaneously choose one of three options: rock, paper, or scissors. If both players choose the same option, then the result is a tie. However, if they choose differently, the winner is determined as follows: • Rock beats scissors, because a rock can break a pair of scissors. • Scissors beats paper, because scissors can cut paper. • Paper beats rock, because a piece of paper can cover a rock. Create a game...
In the game Rock Paper Scissors, two players simultaneously choose one of three options: rock, paper,...
In the game Rock Paper Scissors, two players simultaneously choose one of three options: rock, paper, or scissors. If both players choose the same option, then the result is a tie. However, if they choose differently, the winner is determined as follows: • Rock beats scissors, because a rock can break a pair of scissors. • Scissors beats paper, because scissors can cut paper. • Paper beats rock, because a piece of paper can cover a rock. Create a game...
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...
Many of you probably played the game “Rock, Paper, Scissors” as a child. Consider the following...
Many of you probably played the game “Rock, Paper, Scissors” as a child. Consider the following variation of that game. Instead of two players, suppose three players play this game, and let us call these players A, B, and C. Each player selects one of these three items—Rock, Paper, or Scissors—independent of each other. Player A will win the game if all three players select the same item, for example, rock. Player B will win the game if exactly two...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT