Objective: (Please write in python) Write a program that lets the user play the
ID: 3607567 • 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
from random import randint
def get_ComputerMove():
return randint(1, 3)
def get_playerMove():
while True:
print("1 for Rock")
print("2 for Paper")
print("3 for Scissors")
choice = int(input("Enter your choice: "))
if choice <1 or choice > 3:
print("Please choose from give value")
else:
return choice
def calculate_winner(player, computer):
if player == computer:
return 0
if player == 1 and computer == 3:
return 1
elif player == 3 and computer == 1:
return 2
if player == 2 and computer == 3:
return 2
elif player == 3 and computer == 2:
return 1
if player == 1 and computer == 2:
return 2
elif player == 2 and computer == 1:
return 1
def get_printable_choice(choice):
if choice == 1:
return "Rock"
elif choice == 2:
return "Paper"
elif choice == 3:
return "Scissors"
def main():
computer = get_ComputerMove()
player = get_playerMove()
print("Computer choose: " + get_printable_choice(computer))
print("You choose: " + get_printable_choice(player))
result = calculate_winner(player, computer)
if result == 0:
print("Its a tie")
elif result == 1:
print("You won")
else:
print("Computer win")
choice = input("Do you want to continue (y/n)? ")
if choice == "y":
main()
if __name__ == '__main__':
main()
'''
sample run
'''
# copy pastable code link: https://paste.ee/p/pJORY
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.