Objective: (Please write in python) Write a program that lets the user play the
ID: 3607208 • Letter: O
Question
Objective: (Please write in 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 computer has chosen Rock. If the number 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.
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 the other player chooses scissors, then rock wins. (The rock smashes the scissors.)
If one player chooses scissors the other player chooses paper, then scissors wins. (The scissors cuts paper.)
If one player chooses paper the other player chooses rock, then paper wins. (Paper covers rock.)
If both players make the same choice, it is a tie.
Requirements:
Must have the following for functions, Main(), get_ComputerMove(), get_PlayerMove, calculateWinner()
Your game must loop (continue to allow the user and computer to play)
Explanation / Answer
import random
def get_ComputerMove():
v = random.randint(1,4)
if v == 1:
return "Rock"
if v == 2:
return "Paper"
if v == 3:
return "Scissors"
def getPlayerMove():
inp = input("Enter your choice:")
return inp
def calculateWinner(c,p):
print("You chose:",p)
print("Computer chose:",c)
if p == c:
print("It is a tie")
else:
if (c == "Rock" and p == "Scissors") or (c == "Paper" and p == "Rock") or (c == "Scissors" and p == "Paper"):
print("Computer wins")
else:
print("You win")
def Main():
while True:
c = get_ComputerMove()
p = getPlayerMove()
calculateWinner(c,p)
inp = input("Play again?(y/n)")
if inp == "n":
break
Main()
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.