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
andreev551 [17]
4 years ago
12

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

s follows: 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:
Serggg [28]4 years ago
6 0

Answer:

Here is the Python program:

import random

#this method generates random number from 1 to 3 as computer choice #and if the randomNumber value is equal to 1 then computer chose rock, if #2 this means computer chose paper and otherwise scissors

def computerChoice():

   randomNumber = random.randint(1,3)

   if randomNumber == 1:

       return 'rock'

   elif randomNumber == 2:

       return 'paper'

   else:

       return 'scissors'

# this method checks if user entered his choice from the valid choices that #are rock, paper or scissors

def validChoice(choice):

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

#this method invoked when the user enters invalid choice and the loop in #this method keeps asking user to enter one of the rock paper or scissors #in order to play the game

def invalidChoice(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

#this method decides the winner from computer and user, if and elif #conditions check if the choice entered by the user is same as computer or #different and determines the winner accordingly

def winner(player, computer):

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

       print("rock smashes scissors")

       return True

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

       print("scissors cuts paper")

       return True

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

       print("paper wraps rock")

       return True

   else:

       return False

#contains the computer choice

compChoice = computerChoice()

#contains choice that is input by user

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

#checks if user entered valid choice and calls invalidChoice if user entered #invalid choice which is not from rock paper or scissors

if not validChoice(playerChoice):

   playerChoice = invalidChoice(playerChoice)

#if the choices of user and computer are same then game is played again to #determine the winner

while playerChoice == compChoice:

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

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

   compChoice = computerChoice()

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

   if not validChoice(playerChoice):

       playerChoice = invalidChoice(playerChoice)

#calls winner() method to determine the winner from user and computer

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

if winner(playerChoice, compChoice):

   print('Congratulations! You win!')

else:

   print('You lose! Computer wins!')

       

Explanation:

The program contains 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

You might be interested in
How should you decide what to wear to an interview? What kind of things should be considered?
Anon25 [30]
You should wear something professional. A tie, suit, classy dress, heels. You are more likely to be hired if you make it look like you take pride in your appearance. Also, depending on what kind of job you are applying for, the outfits can vary as well.
5 0
3 years ago
Networks that are designed to connect similar computers that share data and software with each other are called:_______
sergij07 [2.7K]

Networks that are designed to connect similar computers that share data and software with each other are called: Peer to Peer (P2P) Networks.

<h3>What is a networking software?</h3>

A networking software can be defined as a set of executable instructions (codes) that is typically designed and developed to instruct a networking device on how to perform a specific task or transmit data from one location to another.

<h3>The types of networking software.</h3>

Generally, there are three main types of networking software and these include the following:

  • Security software
  • Connection software
  • Transport software

In Computer networking, Peer to Peer (P2P) Networks can be defined as a type of network that are designed and developed to connect similar computers that share data and software with each other.

Read more on Peer to Peer Networks here: brainly.com/question/1932654

#SPJ1

7 0
2 years ago
A sequence of direct connections
stealth61 [152]

Explanation:

Es tut mir nicht so leid

5 0
3 years ago
A spreadsheet contains the maximum weight and maximum height for fifty dog breeds. The breeds are located in rows, and the weigh
MrRa [10]

Answer:

a. Locate

Explanation:

Question options (obtained on the net):

a. Locate

b. Sort

c. Filter

d. Replace

Explanation:

The largest value of the weights of the dog breed in row 43 is the maximum weight of the dog breed in the row

The 'MAX' formula or the 'Autosum' tool can be used to give the largest value in a row, or column

To locate the maximum value in MS Excel, the ADDRESS and MATCH and MAX functions are combined and entered into the blank cell on the right, specifying the same range for the MAX and MATCH functions, and a '0' for the MATCH function as well as a '1' for the ADDRESS function as follows;

=ADDRESS(MATCH(MAX(A1:E1), A1:E1, 0), 1)

Therefore, to navigate to the maximum weight, Emily should use a <em>locate</em>(ing) tool navigate to the information on the spread sheet

8 0
3 years ago
What are possible consequences for cyberbullying?
BaLLatris [955]

Answer:

Possible consequences for cyber bullying include depression, isolation and illness

Explanation:

The reason for this is when a person is hurt there body tends to go through things to make them feel a certain way about things.

3 0
3 years ago
Read 2 more answers
Other questions:
  • What is the first step to creating a PivotTable?
    9·2 answers
  • In linux, you are restricted to using only one type of filesystem on a system.
    15·1 answer
  • "The OSI model has seven layers and the DoD has four. At which layer does SMTP works in both models"?
    15·1 answer
  • Are mechanical keyboards more expensive than membrane keyboards?
    9·2 answers
  • Write a function all_cats that consumes a comma-separated string of animals and prints whether all of the animals have "cat" in
    12·1 answer
  • How to delete Brainly account ?​
    12·1 answer
  • Mika forgot to put in the function name in his function header for the code below. What would be the best function header?
    12·2 answers
  • ____ a British mathematician devolved the concept of a programmable digital computer and worked with Ada Lovelace to design the
    13·1 answer
  • What is the easiest way to deploy a C++ program to a user’s computer? a. Copy the source code to the user’s computer and then co
    13·1 answer
  • A(n) ________ is a wonder of miniaturization combining a CPU, GPU, and sundry other support logic onto a single silicon die, sav
    9·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!