Question
QUESTION AND EXPLANATION IN ITALICS, CODE TO BE DEBUGGED IN BOLD AFTER QUESTION. Debug the code to allow the Rock-Paper-Scissors game to work properly as
QUESTION AND EXPLANATION IN ITALICS, CODE TO BE DEBUGGED IN BOLD AFTER QUESTION.
Debug the code to allow the Rock-Paper-Scissors game to work properly as shown in the sample output below. Note: User input in italics
Rock(1), Paper(2) or Scissors(3)? 4
Invalid input - please enter the number 1, 2 or 3.
Rock(1), Paper(2) or Scissors(3)? 2
You chose Paper
Computer chose Paper
Draw - no winner!
Play again (y|n)? a
Play again (y|n)? y
Rock(1), Paper(2) or Scissors(3)? 3
You chose Scissors
Computer chose Paper
You win - Scissors cut Paper
Play again (y|n)? y
Rock(1), Paper(2) or Scissors(3)? 2
You chose Paper
Computer chose Scissors
You lose - Scissors cut Paper
Play again (y|n)? n
You played 3 games!
Thanks for playing!
CODE TO BE FIXED:
import random # Function to get user's choice def getUserChoice(): user = int(input('Rock(1), Paper(2) or Scissors(3)? ')) while user < 1 or user > 3: print('Invalid input - please enter the number 1, 2 or 3.') user = int(input('Rock(1), Paper(2) or Scissors(3)? ')) return user # Function to ask if play again def askPlayAgain(): ans = "" while ans in ['Y', 'y', 'N', 'n']: ans = input('Play again (y|n)? ') return ans # Function to play the game def playGame(): play = 'y' noGames = 0 selection = ['Rock', 'Paper', 'Scissors'] role = ['crushes', 'covers', 'cut'] # Number codes for rock, paper, scissors ROCK = 1 PAPER = 2 SCISSORS = 3 # build list of all winning combinations winningCombos = [[PAPER, ROCK], [SCISSORS, PAPER], [ROCK, SCISSORS]] # Repeat while the user wants to play while play == 'y' and play == 'Y': # Prompt for and read user's choice user = getUserChoice() # Display user's choice as text to the screen print('You chose', selection[user]) # Randomly generate computer's choice comp = random.randint(1, 3) # Display computer's "choice" as text to the screen print('Computer chose', selection[comp-1]) # Find a winner. # build current combination currentCombo = [user, comp] if comp != user: print('Draw - no winner!') elif currentCombo in winningCombos: print('You win -', selection[user-1], role[user-1], selection[comp-1]) else: print('You lose -', selection[comp-1], role[comp-1], selection[user-1]) noGames = noGames + 1 play = askPlayAgain() print() # Show final results if noGames == 1: print("You played", noGames, "game!") else: print("You played", noGames, "games!") print('Thanks for playing!') # Main entry of the program getUserChoice()
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started