Question: The user's inputs are specified by entering a number: 1 for rock, 2 for paper, 3 for scissors. If the user enters a 4 ,

The user's inputs are specified by entering a number: 1 for rock, 2 for paper, 3 for scissors. If the user enters a 4, the program should print some useful statistics about the series of game results, print a goodbye message, and exit. The program should reject any other inputs with an error message; it should not crash.
On exit, the program computes and prints some final statistics about the play:
number of games completed (don't count games with errors or exit);
number of user wins and the percentage of total games won;
number of user losses and the percentage of total games lost;
number of draws and the percentage of total games that were draws.
Print percentages with one digit after the decimal point. Don't compute or print the statistics if no games were completed, since the percentages would involve dividing by the number of games, which is 0. Instead print a message stating no games were completed. See the sample output below.
Note that you'll need to use several of the constructs you've learned in class including a loop and if statements. You also must define at least two functions (in addition to your main() routine). For example, my version defined the following three functions, though you don't have to use these same ones. (ROCK is a symbolic constant; see the discussion in the Programming Tips section below.)
def playName( play ):
"""For each possible move, return the name to print. For example,
ROCK prints as 'rock'."""
...
def defeats( play1, play2):
"""Boolean valued function that returns True iff play1 defeats
play2."""
...
def printStats( plays, wins, losses, draws ):
"""Print the statistics for the sequence of games played."""
...
Implementing the Machine Play: The user specifies a play by entering a number. The machine must also make a play. Below is the code to implement the machine play. Be sure to copy this code exactly. (This doesn't count as one of your two required functions.) When you need in your code for the machine to return a play, just call machinePlay().
import random
import sys
# If no 'oracle' is supplied on the command line, choose machine plays
# randomly. Otherwise use the supplied oracle.
HAS_ORACLE =( len(sys.argv)>1)
if HAS_ORACLE: MACHINE_ORACLE = sys.argv[1]
# This is a global variable that indexes into the 'oracle' to select
# the next machine play.
machinePlayCounter =0
def machinePlay ():
"""The machine chooses one of the three moves randomly,
unless there's an oracle passed on the command line. Then
we choose machine plays from that."""
global machinePlayCounter
if HAS_ORACLE and machinePlayCounter < len(MACHINE_ORACLE):
play = MACHINE_ORACLE[ machinePlayCounter ]
machinePlayCounter +=1
else:
play = random.choice(["1","2","3"])
return play
You don't really need to understand the code above. But if you'd like to, here's what it does: If the program is run without an argument on the command line as, for example:
> python3 Project1.py
then each play by the machine is selected at random from the three possible legal responses. If, on the other hand, there is an argument given on the command line, e.g.,
> python3 Project1.py 12313223122131
then plays by the machine are chosen in order from the supplied string; arguments on the command line are always interpreted by Python as strings. In computing, an input like this that drives the computation is sometimes called an oracle. The oracle should contains only characters "1","2", and "3". If it runs out, subsequent machine plays are chosen randomly.
None of your other code needs to take notice of the possibility of running the program in these two different ways. But being able to do this is useful; the TAs can supply an oracle to force your code to behave deterministically. This makes it much easier to write a grading script that doesn't have to account for random behavior.
Expected Output:
Below is a sample output for this program. You should match this exactly (for these inputs), except that the machine responses are randomly generated. Note that this is run from the command line. You can run yours from your IDE, but the TAs should be able to run it from the command line.
> python Project1.py
Welcome to a game of Rock, Paper, Scissors!
Choose your play:
Enter 1 for rock;
Enter 2 for paper;
Enter 3 for scissors;
Enter 4 to exit: exit
Illegal play entered. Try again!
Choose your play:
Enter 1 for rock;
Enter 2 for paper;
Enter 3 for scissors;
Enter 4 to exit: 4
No games were completed.
Thanks for playing. Goodbye!
> python Project1.py
Welcome to a game of Rock, Paper, Scissors!
Choose your play:
Enter 1 for rock;
Enter 2 for paper;
Enter 3 for scissors;
Enter 4 to exit: 1
You played rock; your opponent played rock
There's no winner. Try again!
Ch

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!