python Write a program that lets the user play the game of Rock, Paper, Scissors against the computer. The program should work as 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.

Respuesta :

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
Ver imagen mahamnasir

Answer: I do agree with mahamnasir

btw, Nikki, is ur question like this one???? bc i need help with this.

Rock, Paper, Scissors

In this unit, you expanded your Python skills, and now you can really have some fun! In this lab, you are going to create a game that allows the user to play Rock, Paper, Scissors against the computer. If you aren’t familiar with this classic game, here’s how it works: two people (or a person and a computer!) each select either rock, paper, or scissors. The player who chooses the stronger object wins. Here is how the winner is determined:

Rock beats scissors because a rock can break scissors.

Paper beats rock because paper can cover a rock.

Scissors beats paper because scissors can cut paper.

Go to https://repl.it/ to write your program.

Your program should do the following:

Randomly choose rock, paper, or scissors for the computer

Ask the user to choose rock, paper, or scissors

Compare the computer’s choice to the player’s choice

Announce whether the computer or the human won

Here are two hints to help you write this program:

Test parts of your program to see if they work before moving on to work on other parts. For example, you will need to have the computer select a random number and then choose for that number to correspond to rock, paper, or scissors. You can write just that portion of the program and then test it by having the program print the word “rock,” “paper,” or “scissors” before you even begin working on the part of the program where the user inputs their choice. (Of course, don’t forget to erase the line of code that prints the computer’s choice when you are finished testing—otherwise the game will be just a bit too easy for the user!)

On a piece of paper, draw a chart that shows all nine possible outcomes of the game and use that chart to design and test the program. The chart might look like this:

TABLE 1 Rock paper scissors outcomes

 Computer chooses rock Computer chooses paper Computer chooses scissors

User chooses rock

Tied game

Computer wins

User wins

User chooses paper

     

User chooses scissors

     

Run several tests on your program to be sure that the correct winner is announced.

When your program works properly, take a screenshot of your code to turn in.

ACCESS MORE
EDU ACCESS