CS21 Lab 4: while loops and functions

Due Saturday, February 24, before midnight


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/

Programming Tips

Topics for this assignment

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.


1. Pick a choice for rock-paper-scissors

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

2. Play one game of rock-paper-scissors

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.

3. Rock-paper-scissors against the computer.

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:

  1. Prompts the user for their name.
  2. Prompts the user to enter the number of total wins to play until (numWins).
  3. Plays multiple games of rock-paper-scissors against the computer until the first player wins numWins many times.
  4. Prints the first player (the user or the computer) to win numWins many games, and how many total rounds of play it took to win.

Requirements

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):

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.

4. Answer the Questionnaire

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.


Turning in Your Labs

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.