Respuesta :
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.