In: Computer Science
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
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:;