Make sure all programs are saved to your cs21/labs/04
directory. Files outside this directory will not be graded.
$ update21
$ cd ~/cs21/labs/04/
This assignment uses the random
module, in particular the functions random.randrange()
and random.choice()
.
In this lab, we will be writing a program for a human user to play rock-paper-scissors against a computer. We will build this incrementally in several files.
In a file named get-pick.py, write a function named getChoice
that asks a user to enter one of "rock", "paper", or "scissors", and returns the string value entered by the user. If the user doesn't enter one of these three string values, your function should print an error message, and prompt the user to try again. The function should only return when the user enters a valid string value.
The getChoice
function does not take any input values (it has no parameters), but it should return a string value. Its function definition will look like:
def getChoice():
Write a main function that makes a call to your getChoice
function and prints out the value returned. Here are some examples of how the program should work:
$ python3 get-pick.py
Pick one of rock, paper, or scissors: rock
getChoice returned rock
$ python3 get-pick.py
Pick one of rock, paper, or scissors: Paper
Whoops! Paper isn't a valid choice. Try again.
Pick one of rock, paper, or scissors: hey, it should work!
Whoops! hey, it should work! isn't a valid choice. Try again.
Pick one of rock, paper, or scissors: ok, fine
Whoops! ok, fine isn't a valid choice. Try again.
Pick one of rock, paper, or scissors: paper
getChoice returned paper
Remember that python strings can be compared with relational operators like ==
or <
just like integers and floats.
"morning" != "evening" # evaluates to True
First, copy your get-pick.py
solution into a new file named one-game.py
.
$ cp get-pick.py one-game.py
The one-game.py
file will contain your solution to part 1, which you will use as a starting point for part 2. In the one-game.py
file, you will add a new function, calculateWinner
. This function takes as input two string values, the first representing player one's choice (of "rock", "paper", or "scissors"), and the second representing player 2's choice. The function should determine the winner of one game of rock-paper-scissors based on the players' choices, and return a value indicating which player won. The function should return:
Its function definition will look like:
def calculateWinner(player1, player2):
The rules of rock-paper-scissors are:
Here are some example runs:
$ python3 one-game.py
Player 1:
Pick one of rock, paper, or scissors: scissors
Player 2:
Pick one of rock, paper, or scissors: rock
Player 1 picked scissors and Player 2 picked rock
calculateWinner returned 2
Player 2 wins!
$ python3 one-game.py
Player 1:
Pick one of rock, paper, or scissors: papper
Whoops! papper isn't a valid choice. Try again.
Pick one of rock, paper, or scissors: paper
Player 2:
Pick one of rock, paper, or scissors: rock
Player 1 picked paper and Player 2 picked rock
calculateWinner returned 1
Player 1 wins!
$ python3 one-game.py
Player 1:
Pick one of rock, paper, or scissors: rock
Player 2:
Pick one of rock, paper, or scissors: rock
Player 1 picked rock and Player 2 picked rock
calculateWinner returned 0
A tie.
For the final part, you are going to start with your solution to part 2, and add to it code that plays multiple rounds of rock-paper-scissors against the computer.
First, copy your one-game.py
solution into a new file called rock-paper-scissors.py
. The rock-paper-scissors.py
file will contain your solution to this next part.
$ cp one-game.py rock-paper-scissors.py
In the rock-paper-scissors.py
main function you are going to write a program that:
printScores
that takes as input
The function should print out the current scores of the two players between lines of dashes. For example, with the argument values "Beth", 3, and 1, it would print out something like:
------------------------------
Beth: 3 Computer: 1
------------------------------
Note that this function does not return a value. It is called for the side-effect of printing some information. The function definition will look like:
def printScores(name, userWins, compWins):
The main program should make calls to your getChoice
and calculateWinner
functions to get the human player's choice each round, and to compute the winner of each round of play.
You should use the random library (either the randrange
or choice
function) to pick the computer's choice of "rock", "paper" or "scissors" for each round.
After each round, the result of the round (a tie, or which player wins) should be displayed, and your main program should call your printScores
function to print out a summary of the total number of games won so far by each player.
The program should continue playing rounds of the game until the first player (either the user or the computer) wins the specified number of games.
At the end, your program should print the winner (who got to total number of wins first), and the total number of rounds that it took to get there.
All functions should be well commented.
Your program's output does not need to be identical to the following output, but you should make sure to have all the required functionality and that your program displays the information in a readable way.
Sample output:
$ python3 rock-paper-scissors.py
What is your name? Hiro
How many wins should we play until? 2
Let's see who can win 2 games first! Good luck.
Next round:
Pick one of rock, paper, or scissors: rock
Hiro picks rock and Computer picks paper
... Computer wins!
--------------------
Hiro: 0 Computer: 1
--------------------
Next round:
Pick one of rock, paper, or scissors: scissors
Hiro picks scissors and Computer picks paper
... Hiro wins!
--------------------
Hiro: 1 Computer: 1
--------------------
Next round:
Pick one of rock, paper, or scissors: woo!
Whoops! woo! isn't a valid choice. Try again.
Pick one of rock, paper, or scissors: scissors again
Whoops! scissors again isn't a valid choice. Try again.
Pick one of rock, paper, or scissors: scissors
Hiro picks scissors and Computer picks scissors
... A tie.
--------------------
Hiro: 1 Computer: 1
--------------------
Next round:
Pick one of rock, paper, or scissors: rock
Hiro picks rock and Computer picks scissors
... Hiro wins!
--------------------
Hiro: 2 Computer: 1
--------------------
Hiro beat Computer:
... won 2 games in 4 rounds of rock-paper-scissors.
$ python3 rock-paper-scissors.py
What is your name? Esmeralda
How many wins should we play until? 3
Let's see who can win 3 games first! Good luck.
Next round:
Pick one of rock, paper, or scissors: scissors
Esmeralda picks scissors and Computer picks paper
... Esmeralda wins!
--------------------
Esmeralda: 1 Computer: 0
--------------------
Next round:
Pick one of rock, paper, or scissors: paper
Esmeralda picks paper and Computer picks rock
... Esmeralda wins!
--------------------
Esmeralda: 2 Computer: 0
--------------------
Next round:
Pick one of rock, paper, or scissors: paper
Esmeralda picks paper and Computer picks paper
... A tie.
--------------------
Esmeralda: 2 Computer: 0
--------------------
Next round:
Pick one of rock, paper, or scissors: paper
Esmeralda picks paper and Computer picks rock
... Esmeralda wins!
--------------------
Esmeralda: 3 Computer: 0
--------------------
Esmeralda beat Computer:
... won 3 games in 4 rounds of rock-paper-scissors.
Each lab has a short questionnaire at the end. Please edit the QUESTIONS-04.txt
file in your cs21/labs/04
directory and answer the questions in that file.
Don't forget to run handin21
to turn in your lab files! You may run handin21
as many times as you want. Each time it will turn in any new work. We recommend running handin21
after you complete each program or after you complete significant work on any one program.