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
Which feature is needed to make enterprise application migrate to private cloud ?
den301095 [7]

The answer is C: Enablement of application as SaaS.

A cloud can provide IT infrastructure network services like storage and servers. Once an application has been identified as a potential candidate for cloud migration, it is important to consider what type of cloud environment; PaaS, SaaS, or IaaS. In this case, SaaS will be considered. SaaS host business application and makes them available to clients online. It removes the need for enterprises to run applications on their own.

8 0
3 years ago
Read 2 more answers
A.Viruses B.Spyware C.Spam D.Malware<br> Hd
Nookie1986 [14]

I think it is Spam.

But i'm not %100 sure.

3 0
3 years ago
If an LED had power rating of 150 milliwatts at 1.5 volts , how much current is used
Lana71 [14]

The relation between Power, Current and Voltage is given as:

      P = V i

where P is Power in Watts,

V is Voltage in Volts,

and i is Current in Ampere.


We are given:

Power = 150 milliwatts = 0.15 Watts

(as 1 milliwatt = 0.001 Watts)

Voltage = 1.5 volts

and we need to find the current.


Hence using above equation:

P = V i

i = \frac{P}{V}

i = \frac{0.15}{1.5}

i= 0.1 Ampere


Answer:

The current used is 0.1 Ampere

8 0
3 years ago
Electronic computer chips made of tiny silicon wafers now regularly contain millions of electronic switches. Unfortunately, elec
Virty [35]

Answer:

E

Explanation:

if you can reasonably shield them why would you replace the switches

8 0
4 years ago
Ways information technology does not make us productive
VARVARA [1.3K]

Answer:

Although technology is moving the limits, its power is not always helpful.

Explanation:

Technology can make us very non-productive, as it takes time away from our most productive hours when we use our productive time to scroll social media.

Notifications can interrupt the concentration. It makes us lazy in the morning, when, instead of being productive, we are reading anything online.

It can impact our sleep, anxiety, and it may force us to spend time creating a false image of ourselves.

4 0
3 years ago
Other questions:
  • Which of the following refers to applications and technologies that are used to gather, provide access to, and analyze data and
    8·2 answers
  • Which of the following statements is​ FALSE? A. Security is a huge concern for nearly all organizations with private or propriet
    14·1 answer
  • Memories Record Players Company has seen most of its competition slip away, and it now holds 95 percent of the market share for
    5·1 answer
  • Who are the four main antagonists in fnaf 1
    13·2 answers
  • Define full-duplex. Group of answer choices term used to describe the networking device that enables hosts in a LAN to connect t
    9·1 answer
  • 6 external parts of computer
    10·2 answers
  • Papa Mario of Mario's Pizzeria has baked a huge pizza and cut it into n slices, but he is clumsy and the pizza wasn't evenly sli
    14·1 answer
  • For successful completion of an 8-Week course, how often should I visit the course web site to check for new e-mail and any inst
    14·1 answer
  • What city, the major financial center of its country, is located on the northern coast of the island where the poet who wrote "A
    8·2 answers
  • The function changeLocation is also called a _____.
    5·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!