1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
lana66690 [7]
4 years ago
13

Write a program that lets the user play the game of Rock, Paper, Scissors against the computer. The program should work as follo

ws: 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. (Do not display the computer's choice yet) The user enters his or her choice of "rock", "paper, or "scissors" at the keyboard. The computer's choice is displayed. A winner is selected according to the following rules: If one player chooses rock and the other player chooses scissors, then rock wins. (Rock smashes 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 play 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.

Computers and Technology
1 answer:
aleksley [76]4 years ago
7 0

Answer:

Here is the Python program for the game rock, paper, scissors.

import random

def computerChoice():  #function for the computers choice

   randomNumber = random.randint(1,3)  

# generates random no in the range of 1 through 3

   if randomNumber == 1:  #if that random number is equal to 1

       return 'rock'  #computer has chosen rock

   elif randomNumber == 2:  #if random number is equal to 2

       return 'paper' #computer has chosen paper

   else:

       return 'scissors'  #computer has chosen scissors

def validChoice(choice):  #function which checks if user enters right choice

   return choice == 'rock' or choice == 'paper' or choice == 'scissors'

    #the right choice is one of the following rock paper or scissor

def invalidChoice(choice):  #function to check if user entered invalid choice

#loop continues until the user enter the correct given choice

   while choice != 'rock' and choice != 'paper' and choice != 'scissors':

         print('That is not a valid choice.')  

         choice = input('\nEnter rock, paper, or scissors: ')

   return choice

def winner(player, computer): # function to decide who wins the game

   if player == 'rock' and computer == 'scissors':  

#if player chooses rock and computer has chosen scissors

       print("rock smashes scissors")  #display this message

       return True  #function returns true

        #if player chooses scissors and computer has chosen paper

   elif player == 'scissors' and computer == 'paper':

       print("scissors cuts paper")

       return True

     #if player chooses paper and computer has chosen rock

   elif player == 'paper' and computer == 'rock':

       print("paper wraps rock")

       return True          

   else:

       return False  

compChoice = computerChoice()  #stores the choice made by computer

playerChoice = input('Choose from rock, paper, or scissors: ')

#stores what player inputs as his choice

if not validChoice(playerChoice):  #if the choice is not valid

   playerChoice = invalidChoice(playerChoice)  

#calls invalid function to take valid input choice from user

while playerChoice == compChoice:  

#loop continues to execute until the winner is decided

   print('Computer\'s choice:', compChoice)

#if there is a tie then the game is played again to determine the winner

   print('Its a tie!  Choose again.')      

   compChoice = computerChoice()

   playerChoice = input('\nEnter rock, paper, or scissors: ')

   if not validChoice(playerChoice):

       playerChoice = invalidChoice(playerChoice)

#if player wins congratulation message is displayed

print('Computer\'s choice:', compChoice)

if winner(playerChoice, compChoice):

   print('Congratulations! You win!')

else:  #if player loses from computer

   print('You lose! Computer wins!')  

Explanation:

This program has following methods:

computerChoice() This function generates random number in the range of 1 through 3 to take choice from the computer and it uses random.randint() to generate random number from the given range. If the random number is 1 then the computer has chosen rock and if random number is 2 then the computer has chosen paper otherwise scissors.

validChoice() function sets the choice variable to rock, paper and scissors which means user should enter one of these available choices.

invalidChoice() function checks if user entered invalid choice. Invalid choice is anything entered other than rock, paper, scissors. While loop continues to execute and keeps asking to enter a choice until the user enter the correct given choice. It displays That is not a valid choice if user enters an invalid choice.

winner() function determines the winner between computer and player by comparing the choices entered by player and computer. For example if the player enters rock and computer has chosen scissors then this message is displayed rock smashes scissors and function returns true. This means player wins.

Lastly the main program prompts user to enter a choice and stores computer's choice too and compares both the choices to determine the winner accordingly. If there is tie then the game is started again to determine the winner.

The output is attached.

You might be interested in
What is presentation
Gelneren [198K]
To present something infront of a crowd
5 0
3 years ago
Read 2 more answers
Suppose that we have a set of activities to schedule among a large number of lecture halls, where any activity can take place in
laiz [17]

Answer:

By Using the Greedy- Activity- Selection algorithm

Explanation:

The Greedy- Activity- Selection algorithm in this case involves

First finding a maximum size set S1, of compatible activities from S for the first lecture hall.

Then using it again to find a maximum size set S2 of compatible activities from S - S1 for the second hall.

This is repeated till all the activities are assigned.

It requires θ(n2) time in its worse .

8 0
3 years ago
Hi can someone help me make a like a song for music Class pls I attached an example of what i need.
g100num [7]

Answer:

i dont really like music so sorry i cant help i hope someone can help you with this.

8 0
3 years ago
Which of the following is CORRECT about database managementsystem's languages?
scZoUnD [109]

Answer:

Data manipulation languages are used for the retrieval of the,insertion,deletion and modification of data.

Explanation:

Data Manipulation Languages(DML) are used to insert,delete,update,modify the data in the database.

The commands used to do these operations are as following:-

INSERT INTO :-This command is used to insert values in the database.

DELETE:-It is used to delete existing records from the table.

UPDATE:- It is used to modify the records in the table.

SELECT:- It is used to select data from database.

3 0
3 years ago
A self improvement plan should begin with planning how you will accomplish your goal.
Artist 52 [7]

Answer:

True

Explanation: The first step is to make a goal. The second step is to plan how you will accomplish you goal. The third and final step is to accomplish your goal.

4 0
3 years ago
Read 2 more answers
Other questions:
  • What does software alone enable a computer to do? connect to the Internet control processing speeds interact with the user manag
    11·2 answers
  • ANSWER ME PLS
    9·1 answer
  • Windows 1.0 was not considered to be a "true" operating system but rather an operating environment because _____.
    13·1 answer
  • "Consider yourself driving with 60 miles/hour in a city that has only grid like streets, and your GPS is broken.The specificatio
    12·1 answer
  • Return a version of the given string, where for every star (*) in the string the star and the chars immediately to its left and
    5·1 answer
  • Define the following term. data, database, DBMS, database system, data- base catalog, program-data independence, user wen', DBA,
    12·1 answer
  • You work for a company that builds custom PCs for select clients. You are training a new employee to evaluate and select appropr
    13·1 answer
  • Write a qbasic program to display integer numbers 1 to 100 using the for next loop<br>​
    9·1 answer
  • Why is science, mathematics, and technology different from engineering?
    15·1 answer
  • Need answer ASAP
    8·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!