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
in python Write a loop that counts the number of lowercase characters that appear in the string referenced by mystring.
exis [7]

Answer:

mystring = "My name is David Acheme"

count = 0

for i in mystring:

    if(i.islower()):

        count=count+1

print("The number of lowercase characters in: "+mystring)

print(count)

Explanation:

1. We declare a String Variable and assign a value.

2. Intialize a count variable to zero.

3. Using a for loop is to loop through each characters in the string.

4. The if condition checks when a lower case character is encountered using the islower method and increases count by 1

5. Prints the value of count.

5 0
4 years ago
Which files have not been processed by your camera or computer?
777dan777 [17]

I think it is C. Is that help

3 0
4 years ago
Ancler Corporation wants to calculate how much their profits were in the first quarter of the year. They should use a
Wewaii [24]

They should use a spreadsheet.

Explanation:

With a spreadsheet all they will need to do is to give formula to the cells and any time they key in the expenses and revenue figures they are going to get their profit for the quarter.

It is more convenient and reliable. It reliefs the user of computation issues.

6 0
3 years ago
Read 2 more answers
Will give Brainliest 100 points answer only if you know
katrin [286]
There are five basic programming elements, or operations: input, output, arithmetic, conditional, and looping. Every program uses at least two of these.

The description of a programming language is usually split into the two components of syntax (form) and semantics (meaning).

Input: getting data and commands into the computer

Output:getting your results out of the computer

Arithmetic: performing mathematical calculations on your data

Conditional: testing to see if the condition is true or false

Looping: cycling through a set of instructions until some condition is met
7 0
3 years ago
Read 2 more answers
A foreach statement can be used in place of the for statement whenever code looping through an array requires "access" to the co
Ede4ka [16]

Answer:

False

Explanation:

Foreach is useful when we want to do something for each element rather than repeating something for  n times

Answer is False

5 0
3 years ago
Other questions:
  • Answer how do you properly dispose of a large box of assorted computer parts, including hard drives, optical drives, computer ca
    11·1 answer
  • Three healthcare firms jointly own and share the same cloud resources to meet their computing needs. Which cloud model does this
    15·1 answer
  • The first thing you must consider in any type of communication is ______.
    9·2 answers
  • Tech Town has been hit with a bad storm and the town is suffering from severe flooding. The kids have no way to get to school.Te
    7·1 answer
  • Match the different stages of boot sequence with the order in which they occur.​
    15·2 answers
  • HEEEEEEEEEEEEEEEEELLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLPPPPPPPPPPPPPPPPPPPPPPPPPPP
    12·2 answers
  • Read the following scenario, and then decide if you should upgrade the computer. You have a desktop that you use primarily for w
    7·1 answer
  • How do you think the blitz might have affected civilian morale in london
    15·1 answer
  • Which of the following tabs on the Ribbon contains the command to add a Quick Part to a document? A Design B Insert C View D Hom
    7·1 answer
  • Fast i need now ill give anything
    13·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!