Rock, Paper, Scissors! Write a program which let's the user play rock, paper, scissors against the computer. It should: Ask for the user's name, and then use that name in output for scoring and messages Randomly generate the computer's choice ("rock", "paper" or "scissors") Ask the user to enter their choice ("rock", "paper" or "scissors") Display the computer's choice Compare the choices, then determine a winner: rock beats scissors paper beats rock scissors beat paper a tie results in a do-over until a winner is declared The winner (user or computer) gets a point After each round, display the total score for the user and the computer Ask if the user wants to play again If yes, then display another round of the game. Don't ask for the user's name again. If no, then display the total number of rounds played and the final score

Respuesta :

Answer:

import random

def getComputerChoice(num):

   if num == 1:

       return "rock"

   if num == 2:

       return "paper"

   return "scissor"

def main():

   name = input("Enter your name: ")

   rounds = 0

   com_wins = 0

   user_wins = 0

   while True:

       print("\n1.Rock")

       print("2.Paper")

       print("3.Scissor")

       user = int(input("Enter choice: "))

       rounds += 1

       while user<=0 or user>3:

          print("Invalid choice")

          user = int(input("Enter choice: "))

       computer_choice = random.randint(1,3)

       # This is for handling tie

       while computer_choice == user:

           computer_choice = random.randint(1,3)

       print("Computer has chosen ->",getComputerChoice(computer_choice))

       if (computer_choice==1 and user==2) or (computer_choice==2 and user==1):

           print("paper beats rock")

           winner = 2

       elif (computer_choice==3 and user==1) or (computer_choice==1 and user==3):

           print("rock beats scissor")

           winner = 1

       else:

           print("scissors beat paper")

           winner = 3

       if winner == user:

           print(name,"wins!!!")

           user_wins += 1

       else:

           print("Computer wins!!!")

           com_wins += 1

       play = input("Do you want to play again?(Y/N) ")

       if play == 'n' or play == 'N':

           break

   print("No of rounds played:",rounds)

   print("Computer won:", com_wins)

   print(name,"won:",user_wins)

main()

ACCESS MORE
EDU ACCESS