Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Using Python 3.4 - Create a menu-driven rock, paper, scissors game that a user p

ID: 665966 • Letter: U

Question

Using Python 3.4 - Create a menu-driven rock, paper, scissors game that a user plays against the computer with the ability to save and load a game and its associated play statistics.

Requirements

For menus in the game, the user makes a selection from the menu by entering the number of their choice. If the user enters something other than a number or a number in the range of the choices provided in the menu they are to be given feedback that their choice is not valid and asked for the input again. Under no circumstance should input from the user crash the program. When the program is run the following title, menu, and input prompt are to be displayed: Welcome to Rock, Paper, Scissors! 1. Start New Game 2. Load Game 3. Quit Enter choice:

1. Start New Game

If the user chooses to start a new game the program is to prompt the user for their name using the following prompt: What is your name? After the name is entered the program is to respond with a line that is “Hello” followed by the name of the user, a period, and the phrase “Let’s play!” Example: Hello Justin. Let’s play! After the hello statement is presented to the user, game play is to proceed.

2. Load Game

If the user chooses to load an existing game the program is to prompt the user for their name using the following prompt: What is your name? After the name is entered the program is to attempt to load a game file that is named based on the user name with an extension of .rps. For example, the file name for Justin is to be Justin.rps. If the file is found, the information about the game and statistics are to be loaded, a welcome back message is to be presented to the user, and game play is to proceed. The round number displayed is to be based on the number of rounds previously played. (Note: number of rounds previously played is sum of wins, losses, and ties.) Game play is described below in the Game Play section. The welcome back message is to be “Welcome back “ followed by the user’s name, a period, and the phrase “Let’s Play!” Example: Welcome back Justin. Let’s play! If the file is not found, the user is to be presented with a message indicating the game could not be found and then presented with the startup menu described at the top of the requirements. The message is to be the user name followed by “, your game could not be found.” Example: Justin, your game could not be found.

3. Quit If the user chooses to quit, the program is to exit.

Game Play

For each round a line that includes the round number is to be displayed followed by a menu that let’s the user choose Rock, Paper, or Scissors as their choice for the round as shown here: Round (round number) 1. Rock 2. Paper 3. Scissors What will it be? The user makes their choice and the computer chooses randomly. The result of the round is to be displayed using the following format: You chose . The computer chose . You ! Example: You chose Paper. The computer chose Rock. You win! After the round the user is to be presented with the following prompt and menu: What would you like to do? 1. Play Again 2. View Statistics 3. Quit Enter choice: If the user chooses 1 to play again, the user is to play another round. If the user chooses 2 to view statistics, the current statistics for the game are to be displayed. If the user chooses 3 to quit, the game and associated statistics are to be saved to a file and the program is to exit.

1. Play Again If the user chooses to play again the next round number is to be displayed and the user is to again choose Rock, Paper, or Scissors to play the round as described above. 2. View Statistics If the user chooses to view the statistics the following information is to be displayed: , here are your game play statistics... Wins: Losses: Ties: Win/Loss Ratio: Example: Justin, here are your game play statistics... Wins: 8 Losses: 7 Ties: 2 Win/Loss Ratio: 1.14 After the statistics are displayed the user is to be presented with the menu shown above that is presented after playing a round: What would you like to do? 1. Play Again 2. View Statistics 3. Quit Enter choice:

3. Quit

If the user chooses to quit the game the program is to automatically save the game and associated statistics in a game file that is named based on the user name with an extension of .rps. For example, the file name for Justin is to be Justin.rps. If there is an exception saving the file an error message reporting the error is to be presented to the user. The message is to be “Sorry “ followed by the user’s name and “, the game could not be saved.” Additionally the actual error provided by the Exception object is to be displayed on the next line. Example: Sorry Justin, the game could not be saved. If the file saves successfully the user is to be presented with a message indicating the success of the file save. The message is to be the user’s name followed by “, your game has been saved.” Example: Justin, your game has been saved. Finally, the program is to exit.

Explanation / Answer

import random

#import some system specific parameters and functions using
#the sys module
import sys


def makeYourChoice():

    print "Press R for Rock"
    print "Press P for Paper"
    print "Press S for Scissors"
    print "Press Q to quit!"

    userChoice = raw_input("What do you want to choose? ").lower()

    if userChoice == "r":
        return "Rock"

    if userChoice == "p":
        return "Paper"

    if userChoice == "s":
        return "Scissors"

    if userChoice == "q":
        sys.exit(0)

    else:
        makeYourChoice()


def computerRandom():

    options = ["Rock","Paper","Scissors"]
    randomChoice = random.randint(0,2)
    return options[randomChoice]

def comparison(humanChoice, computerChoice):

    if humanChoice == computerChoice:
        return "Draw"

    if humanChoice == "Rock" and computerChoice == "Paper":
        return "Computer Wins"

    if humanChoice == "Paper" and computerChoice == "Scissors":
        return "Computer Wins"

    if humanChoice == "Scissors" and computerChoice == "Rock":
        return "Computer Wins"

    else: return "Human Wins"


humanChoice = makeYourChoice()
computerChoice = computerRandom()


print "You chose: ", humanChoice
print "The computer chose: ", computerChoice

result = comparison (humanChoice, computerChoice)


if result == "Draw":
print "Its a draw"

elif result == "Computer Wins":
   print "Unlucky you lost!"

else:
   print "Well done you won!"

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote