Rock, Paper, Scissors Game
Write a program that lets the user play the game of Rock, Paper, Scissors against the computer.
The program should work as follows.
1. 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. (Don’t display the computer’s choice yet.)
2. The user enters his or her choice of “rock”, “paper”, or “scissors” at the keyboard.
3. The computer’s choice is displayed.
4. A winner is selected according to the following rules:
Rock, Paper, Scissors Game Program code:
#Header section of random
import random
# 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.
#The user enters his or her choice of “rock”, “paper”, or
#“scissors” at the keyboard. The computer’s choice is displayed.
def findWinner(playerChoice, computerChoice):
if playerChoice == computerChoice:
return "It's a tie!"
if (playerChoice == "rock" and computerChoice == "scissors") or \
(playerChoice == "scissors" and computerChoice == "paper") or \
(playerChoice == "paper" and computerChoice == "rock"):
return "You(player) win!"
else:
return "Computer wins!"
def play_game():
choices = ["rock", "paper", "scissors"]
computerChoice = random.choice(choices)
while True:
playerChoice = input("Enter your choice (rock/paper/scissors): ").lower()
if playerChoice in choices:
print("Computer's choice:", computerChoice)
print(findWinner(playerChoice, computerChoice))
break
else:
print("Invalid choice. Please enter rock, paper, or scissors.")
play_game()
Executed Output 1:
Enter your choice (rock/paper/scissors):
rock
Computer's choice: scissors
You(player) win!
Executed Output 2:
Enter your choice (rock/paper/scissors):
scissors
Computer's choice: paper
You(player) win!