4,358 views
26 votes
26 votes
Write a Python program stored in a file q1.py to play Rock-Paper-Scissors. In this game, two players count aloud to three, swinging their hand in a fist each time. When both players say three, the players throw one of three gestures: Rock beats scissors Scissors beats paper Paper beats rock Your task is to have a user play Rock-Paper-Scissors against a computer opponent that randomly picks a throw. You will ask the user how many points are required to win the game. The Rock-Paper-Scissors game is composed of rounds, where the winner of a round scores a single point. The user and computer play the game until the desired number of points to win the game is reached. Note: Within a round, if there is a tie (i.e., the user picks the same throw as the computer), prompt the user to throw again and generate a new throw for the computer. The computer and user continue throwing until there is a winner for the round.

User Gedas Kutka
by
3.2k points

1 Answer

11 votes
11 votes

Answer:

The program is as follows:

import random

print("Rock\\Paper\\Scissors")

points = int(input("Points to win the game: "))

player_point = 0; computer_point = 0

while player_point != points and computer_point != points:

computer = random.choice(['Rock', 'Paper', 'Scissors'])

player = input('Choose: ')

if player == computer:

print('A tie - Both players chose '+player)

elif (player.lower() == "Rock".lower() and computer.lower() == "Scissors".lower()) or (player.lower() == "Paper".lower() and computer.lower() == "Rock".lower()) or (player == "Scissors" and computer.lower() == "Paper".lower()):

print('Player won! '+player +' beats '+computer)

player_point+=1

else:

print('Computer won! '+computer+' beats '+player)

computer_point+=1

print("Player:",player_point)

print("Computer:",computer_point)

Step-by-step explanation:

This imports the random module

import random

This prints the three possible selections

print("Rock\\Paper\\Scissors")

This gets input for the number of points to win

points = int(input("Points to win the game: "))

This initializes the player and the computer point to 0

player_point = 0; computer_point = 0

The following loop is repeated until the player or the computer gets to the winning point

while player_point != points and computer_point != points:

The computer makes selection

computer = random.choice(['Rock', 'Paper', 'Scissors'])

The player enters his selection

player = input('Choose: ')

If both selections are the same, then there is a tie

if player == computer:

print('A tie - Both players chose '+player)

If otherwise, further comparison is made

elif (player.lower() == "Rock".lower() and computer.lower() == "Scissors".lower()) or (player.lower() == "Paper".lower() and computer.lower() == "Rock".lower()) or (player == "Scissors" and computer.lower() == "Paper".lower()):

If the player wins, then the player's point is incremented by 1

print('Player won! '+player +' beats '+computer)

player_point+=1

If the computer wins, then the computer's point is incremented by 1

else:

print('Computer won! '+computer+' beats '+player)

computer_point+=1

At the end of the game, the player's and the computer's points are printed

print("Player:",player_point)

print("Computer:",computer_point)

User Blackp
by
2.8k points