Question

Write a program in python that lets the user play the game Rock, Paper, Scissors against...

Write a program in python that lets the user play the game 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 the computer has chosen rock. 2 corresponds to paper, and 3 corresponds to scissors.
  • To set up the random number library, write the following at the top of your code:
import random
random.seed(300)
  • Use number = random.randint(1, 100) to generate your random numbers
  • The user chooses rock, paper, scissors, or to exit the game. Tell the user to enter 1-3 for their choice and input it as a number, or 4 if they want to exit.
  • Begin a while loop for the game that exits if they chose 4.
  • The computer's choice is displayed, mapping its numerical choice to the correct string (use if statements).
  • The player's choice is displayed.
  • A winner is selected and outputted using the following rules:
    • Rock beats scissors
    • Scissors beats paper
    • Paper beats rock
    • If both players make the same choice, it is a tie.
  • The program generates a new computer choice, asks the user for a new menu choice, and loops to play a new game if they didn't select 4 to exit.

Use the following functions in your design. They will be unit tested:

getPlayerChoice() 
  • print menu to ask user choice and return as Integer
getComputerChoice()
  • generate random computer choice and return as Integer
determineWinner(computerChoice, playerChoice)
  • Compare the two choices and return a string, containing one of the following:
    • "Player wins!"
    • "Computer wins!"
    • "Tie!"
  • Use its return value to print the winner of the game.
convertChoice(choice); 
  • return "rock", "paper", or "scissors" as a string, depending on the Integer parameter. Use its return value to print the player or the computer's choice.
main()
  • main function that will contain your game loop and call the other functions
  • For main to execute properly when your script runs, the Python interpreter will need this statement at the end of your file:
if __name__ == '__main__':
    main()

Your file main.py will look like this:

import random
random.seed(300)

def determineWinner(playerChoice, computerChoice):
    ```statements```

def convertChoice(choice):
    ```statements```

def getComputerChoice():
    ```statements```

def getPlayerChoice():
    ```statements```

# Main
def main():
    #get the player choice
    #generate the computer choice
    #while the user has not chosen 4
        #print the player choice as a string
        #print the computer choice as a string
        #print the winner as a string
        #get new choices for the player and computer

if __name__ == '__main__':
    main()
0 0
Add a comment Improve this question Transcribed image text
Answer #1
import random

random.seed(300)

def determineWinner(playerChoice, computerChoice):
    # Check who won and print
    if computerChoice == 1 and playerChoice == 2:
        return "Player wins!"
    elif computerChoice == 2 and playerChoice == 1:
        return "Computer wins!"
    elif computerChoice == 3 and playerChoice == 1:
        return "Player wins!"
    elif computerChoice == 1 and playerChoice == 3:
        return "Computer wins!"
    elif computerChoice == 2 and playerChoice == 3:
        return "Player wins!"
    elif computerChoice == 3 and playerChoice == 2:
        return "Computer wins!"
    else:
        return "Tie!"

def convertChoice(choice):
    # Return respective value to number
    if choice == 1:
        return 'rock'
    elif choice == 2:
        return 'paper'
    else:
        return 'scissors'

def getComputerChoice():
    return random.randint(1, 3)

def getPlayerChoice():
    # Ask user to enter either of four
    choice = int(input('1. rock, 2. paper, 3. scissors or 4 to exit: '))
    # Keep asking till user enters a valid input
    while choice not in [1, 2, 3, 4]:
        choice = int(input('1 .rock, 2. paper, 3. scissors or 4 to exit: '))

    return choice


# Main
def main():
    player = getPlayerChoice()  # get the player choice
    comp = getComputerChoice()  # generate the computer choice
    while player != 4:  # while the user has not chosen 4
        # print the player choice as a string
        print('Player''s Choice:', convertChoice(player))
        # print the computer choice as a string
        print('Computer''s Choice:', convertChoice(comp))
        # print the winner as a string
        print(determineWinner(player, comp))

        print()
        # get new choices for the player and computer
        player = getPlayerChoice()
        comp = getComputerChoice()


if __name__ == '__main__':
    main()

SCREENSHOT

OUTPUT

Add a comment
Know the answer?
Add Answer to:
Write a program in python that lets the user play the game Rock, Paper, Scissors against...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • (Java) Write a program that lets the user play the game of Rock, Paper, Scissors against...

    (Java) 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 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 computer’s choice yet. The...

  • IN JAVA. Write a program that lets the user play the game of Rock, Paper, Scissors...

    IN JAVA. 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 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 computer’s choice yet....

  • (c++ only)Write a program that lets the user play the game of Rock, Paper, Scissors against...

    (c++ only)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, the user enters his or her choice of “rock”, “paper”, or “scissors” at the keyboard using a menu in a function, userChoice, that returns a character. Next, there should be a function, computerChoice, that generates the computer’s play. A random number in the range of 1 through 3 is generated. If the...

  • Write a Python program (rock_paper_scissors.py) that allows two players play the game rock paper scissors. Remember...

    Write a Python program (rock_paper_scissors.py) that allows two players play the game rock paper scissors. Remember the rules: 1. Rock beats scissors 2. Scissors beats paper 3. Paper beats rock The program should ask the users for their names, then ask them for their picks (rock, paper or scissors). After that, the program should print the winner's name. Note, the players may pick the same thing, in this case the program should say it's tie. when the user input an...

  • C++ Part 2: Rock Paper Scissors Game Write a program that lets the user play this...

    C++ Part 2: Rock Paper Scissors Game Write a program that lets the user play this game against the computer. The program should work as follows: When the program begins, a random number between 1 and 3 is generated. If the number is 1, the computer has chosen rock. If the number is 2, the computer has chosen paper. If the number is 3, the computer has chosen scissors. Don't display the computer's choice yet. Use a menu to display...

  • In python language Write a program that lets the user play the game of Rock, Paper,...

    In python language Write a program that lets 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 and 3 is generated. 1 = Computer has chosen Rock 2 = Computer has chosen Paper 3 = Computer has chosen Scissors (Dont display the computer's choice yet) 2. The user enters his or her choice of “Rock”, “Paper", “Scissors" at...

  • It's writing a simple rock paper scissors game strictly following the instructions. INSTRUCTIONS: If the user...

    It's writing a simple rock paper scissors game strictly following the instructions. INSTRUCTIONS: If the user selects 'p': 1. First the program should call a function named getComputerChoice to get the computer's choice in the game. The getComputerChoice function should generate a random number between 1 and 3. If the random number is 1 the computer has chosen Rock, if the random number is 2 the user has chosen Paper, and if the random number is 3 the computer has...

  • JAVA Beginnings of a paper-rock-scissors game. Paper-rock-scissors is a game often used to make decisions. There...

    JAVA Beginnings of a paper-rock-scissors game. Paper-rock-scissors is a game often used to make decisions. There are two players. Each player secretly chooses either, paper, rock or scissors. At the count of three, each player reveals what they have chosen. The winner of the game is determined this way: paper beats rock (because paper covers rock) rock beats scissors (because rock crushes scissors) scissors beat paper (because scissors cut paper) If the two players choose the same item, it is...

  • Write a Python program (using python 3.7.2) that lets the user play the game of Rock,...

    Write a Python program (using python 3.7.2) that lets 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...

  • In JAVA Chapter 5Assignment(Rock, Paper, Scissors)–20pointsYour goal is towrite a program that lets the user play...

    In JAVA Chapter 5Assignment(Rock, Paper, Scissors)–20pointsYour goal is towrite a program that lets the user play the game of Rock, Paper, Scissors against the computer.Your program should have the following: •Make the name of the project RockPaperScissors•Write a method that generates a random number in the range of 1 through 3. The randomly generated number will determine if the computer chooses rock, paper, or scissors. If the number is 1, then the computer has chosen rock. If the number is...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT