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