Question

In: Computer Science

Please use Python 21. Rock, Paper, Scissors Game Write a program that let's the user play...

Please use Python

21. Rock, Paper, Scissors Game Write a program that let's 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 computers 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: - If one player chooses rock and the other player chooses scissors, then rock wins. (The rock smashes the scissors.) - If one player chooses scissors and the other player chooses paper, then scissors wins. (Scissors cuts paper.) - If one player chooses paper and the other player chooses rock, then paper wins. (Paper wraps rock) - If both players make the same choice, the game must be played again to determine the winner.

Here is what I have but I keep getting an error.

import random

def generateRandomNumber():
randomNumber = random.randint(1, 3)
return randomNumber

def getComputerChoice( randomNumber ):
if randomNumber == 1:
computerChoice = "rock"
elif randomNumber == 2:
computerChoice = "paper"
elif randomNumber == 3:
computerChoice = "scissors"
  
return computerChoice

def getUserChoice():
userChoice = input("please enter your choice:")
return userChoice

def determineWinner( userChoice, computerChoice):
rockMessage = "The rock smashes the scissors"
scissorMessage = "Scissors cuts paper"
paperMessage = "Paper wraps rock"
winner = "no winner"
message = ""
  
if computerChoice == "rock" and userChoice == "scissors":
winner = "Computer"
message = rockMessage
elif computerChoice == "scissors" and userChoice == "rock":
winner = "You"
message = rockMessage
  
if computerChoice == "scissors" and userChoice == "paper":
winner = "Computer"
message = scissorMessage
elif computerChoice == "paper" and userChoice == "scissors":
winner = "You"
message = scissorMessage
  
if computerChoice == "paper" and userChoice == "rock":
winner = "Computer"
message = paperMessage
elif computerChoice == "rock" and userChoice == "paper":
winner = "You"
message = paperMessage
  
return winner, message

def startAgain():
randomNumber = generateRandomNumber()
computerChoice = getComputerChoice( randomNumber)
userChoice = getuserChoice()
print( "The computer chose", computerChoice)
winner, message = determineWinner( computerChoice, userChoice )
  
if winner != "no winner":
print( winner, "won(", message, ")" )
  
return winner

def main():
randomNumber = generateRandomNumber()
computerChoice = getComputerChoice( randomNumber)
userChoice = getuserChoice()
print( "The computer chose", computerChoice)
winner, message = determineWinner( computerChoice, userChoice )
  
if winner != "no winner":
print( winner, "won(", message, ")" )
  
while winner == "no winner":
winner = startAgain()
main()

NameError                                 Traceback (most recent call last)
<ipython-input-2-a1b2d5e39fa7> in <module>
     73     while winner=="no winner":
     74         winner = startAgain()
---> 75 main()

<ipython-input-2-a1b2d5e39fa7> in main()
     64     randomNumber = generateRandomNumber()
     65     computerChoice = getComputerChoice( randomNumber)
---> 66     userChoice = getuserChoice()
     67     print( "The computer chose", computerChoice)
     68     winner, message = determineWinner( computerChoice, userChoice )

NameError: name 'getuserChoice' is not defined

Solutions

Expert Solution

in line 49 and 59(refer to screenshot) change getuserChoice() to getUserChoice()

Here you have to use Capital 'u' not small u in line 49 and 59

Source code:

import random

def generateRandomNumber():
randomNumber = random.randint(1, 3)
return randomNumber

def getComputerChoice( randomNumber ):
if randomNumber == 1:
computerChoice = "rock"
elif randomNumber == 2:
computerChoice = "paper"
elif randomNumber == 3:
computerChoice = "scissors"
return computerChoice

def getUserChoice():
userChoice = input("please enter your choice:")
return userChoice

def determineWinner( userChoice, computerChoice):
rockMessage = "The rock smashes the scissors"
scissorMessage = "Scissors cuts paper"
paperMessage = "Paper wraps rock"
winner = "no winner"
message = ""   
if computerChoice == "rock" and userChoice == "scissors":
winner = "Computer"
message = rockMessage
elif computerChoice == "scissors" and userChoice == "rock":
winner = "You"
message = rockMessage
if computerChoice == "scissors" and userChoice == "paper":
winner = "Computer"
message = scissorMessage
elif computerChoice == "paper" and userChoice == "scissors":
winner = "You"
message = scissorMessage
if computerChoice == "paper" and userChoice == "rock":
winner = "Computer"
message = paperMessage
elif computerChoice == "rock" and userChoice == "paper":
winner = "You"
message = paperMessage
return winner, message

def startAgain():
randomNumber = generateRandomNumber()
computerChoice = getComputerChoice( randomNumber)
userChoice = getUserChoice()
print( "The computer chose", computerChoice)
winner, message = determineWinner( computerChoice, userChoice )
if winner != "no winner":
print( winner, "won(", message, ")" )
return winner

def main():
randomNumber = generateRandomNumber()
computerChoice = getComputerChoice( randomNumber)
userChoice = getUserChoice()
print( "The computer chose", computerChoice)
winner, message = determineWinner( computerChoice, userChoice )
if winner != "no winner":
print( winner, "won(", message, ")" )
while winner == "no winner":
winner = startAgain()
main()

Sample input and output:;


Related Solutions

Rock, Paper, Scissors Game Write a Python program rps.py that lets the user play the game...
Rock, Paper, Scissors Game Write a Python program rps.py that lets the user play the game of Rock, Paper, Scissors against the computer. The program should work as follows: You can set these constant global variables at the top outside of your main function definition: COMPUTER_WINS = 1 PLAYER_WINS = 2 TIE = 0 INVALID = 3 ROCK = 1 PAPER = 2 SCISSORS = 3 For this program 1 represents rock, 2 represents paper, and 3 represents scissors. In...
Please write a scheme code for the rock paper scissors game between a player and the...
Please write a scheme code for the rock paper scissors game between a player and the computer (AI). In scheme programming language please.
Subjects: Write a Python program that allows users to play the popular rock-paper-scissor game against the...
Subjects: Write a Python program that allows users to play the popular rock-paper-scissor game against the computer multiple times. This program assesses your knowledge of decision structures, repetition structures, and functions. Requirements: 1.Define a function named as play_game. This function receives two parameters representing the player’s and computer’s choices, and it returns an integer representing the game result from the player’s perspective. Specifically, it returns 1 if the player wins and 0 if the player does not win. Hint: you...
Write a program using Python that allows the user to play a guessing game (please provide...
Write a program using Python that allows the user to play a guessing game (please provide a picture of code so it is easier to read). The game will choose a "secret number", a positive integer less than 10000. The user has 10 tries to guess the number. Requirements: Normally, we would have the program select a random number as the "secret number". However, for the purpose of testing your program (as well as grading it), simply use an assignment...
One file java program that will simulate a game of Rock, Paper, Scissors. One of the...
One file java program that will simulate a game of Rock, Paper, Scissors. One of the two players will be the computer. The program will start by asking how many winning rounds are needed to win the game. Each round will consist of you asking the user to pick between rock, paper, and scissors. Internally you will get the computers choice by using a random number generator. Rock beats Scissors, Paper beats Rock, and Scissors beats Paper. You will report...
Write a Java class that determines the winner of a rock, paper scissors game. Assume the...
Write a Java class that determines the winner of a rock, paper scissors game. Assume the input from the user is always valid (so no need to check), that is it contains either one of `R`, `P`, or `S` as a single character, or has matching parenthesis, like, `(S&P)` or `((R&P)&S)`, and the `&` character. So for example, the user inputs `(P&R)` and the program will output `P` since paper beats rock. Or if the user inputs `((S&R)&(S&S))` the output...
Two players play a rock-paper-scissors game. The losing player will give $1 to the winning player....
Two players play a rock-paper-scissors game. The losing player will give $1 to the winning player. If it is a draw, no payment is made. The payoff to a player is the amount of money (s)he gets. Represent the situation in a matrix form. Find all the Nash equilibria.
Solve the scissors, paper, rock game. This game is well known in many parts of the...
Solve the scissors, paper, rock game. This game is well known in many parts of the world. Two players simultaneously present a hand in one of three positions: an open hand (paper), a closed fist (rock), or two open fingers (scissors). The payoff is 1 unit according to the rule “Paper covers rock, rock breaks scissors, and scissors cut paper.” If both players present the same form, the payoff is 0. Set up the payoff matrix for the game and...
In the game Rock Paper Scissors, two players simultaneously choose one of three options: rock, paper,...
In the game Rock Paper Scissors, two players simultaneously choose one of three options: rock, paper, or scissors. If both players choose the same option, then the result is a tie. However, if they choose differently, the winner is determined as follows: • Rock beats scissors, because a rock can break a pair of scissors. • Scissors beats paper, because scissors can cut paper. • Paper beats rock, because a piece of paper can cover a rock. Create a game...
In the game Rock Paper Scissors, two players simultaneously choose one of three options: rock, paper,...
In the game Rock Paper Scissors, two players simultaneously choose one of three options: rock, paper, or scissors. If both players choose the same option, then the result is a tie. However, if they choose differently, the winner is determined as follows: • Rock beats scissors, because a rock can break a pair of scissors. • Scissors beats paper, because scissors can cut paper. • Paper beats rock, because a piece of paper can cover a rock. Create a game...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT