CS21 Lab 4: functions and while loops

Due Saturday, Oct 10, before midnight

Programming Tips

As you write your first programs, start using good programming practices now:

  • Use a comment at the top of the file to describe the purpose of the program (see example).

  • All programs should have a main() function (see example).

  • Use variable names that describe the contents of the variables.

  • Write your programs incrementally and test them as you go. This is really crucial to success: don’t write lots of code and then test it all at once! Write a little code, make sure it works, then add some more and test it again.

  • Don’t assume that if your program passes the sample tests we provide that it is completely correct. Come up with your own test cases and verify that the program is producing the right output on them.

  • Avoid writing any lines of code that exceed 80 columns. In emacs, at the bottom, center of the window, there is an indication of both the line and the column of the cursor.

Function Comments

All functions should have a top-level comment! Please see our function example page if you are confused about writing function comments.

Are your files in the correct place?

Make sure all programs are saved to your cs21/labs/04 directory! Files outside that directory will not be graded.

$ update21
$ cd ~/cs21/labs/04
$ pwd
/home/username/cs21/labs/04
$ ls
Questions-04.txt
(should see your program files here)

Goals

  • Write programs with multiple functions.

  • Solve problems using indefinite while loops.

  • Learn to use the random library to make pseudo-random choices.

1. Checkpoint

At the end of your lab session, be sure to run handin21.

Your lab instructor will check your files, which should reflect that you have made non-trivial progress (in python or pseudocode) towards the solution. Note that if you have not made much progress towards your solution, we expect that you would have been actively seeking help during your lab over Slack.

A portion of your final lab grade is dependent on this checkpoint.

If there are circumstances which prevent you from making substantial progress on this lab, please contact your lab instructor as soon as possible.

2. Get a choice for rock-paper-scissors

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. As such, we’ll be modifying the main function as we move from step to step. There’s no need to save your main function from a previous step as you work on each next step.

In the file named rps.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():

Your main function should make a call to the getChoice function and print out the value returned. Here are some examples of how the program should work:

$ python3 rps.py
Pick one of rock, paper, or scissors: rock
getChoice returned rock

$ python3 rps.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

3. Play one game of rock-paper-scissors

Let’s extend our program to add a function that plays one game of rock-paper-scissors.

Remember, we are solving this incrementally so there’s no need to preserve your main function as you develop the solution to this next step.

To play a game, add a new function called 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 two’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:

0 if the game ends in a tie
1 if player one wins
2 if player two wins

Its function definition will look like:

def calculateWinner(player1, player2):

The rules of rock-paper-scissors are:

  • if both players choose the same thing, the game is a tie

  • rock beats scissors (rock breaks scissors)

  • scissors beat paper (scissors cut paper)

  • paper beats rock (paper wraps rock)

Here are some example runs:

$ python3 rps.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 rps.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 rps.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.

4. Play rock-paper-scissors against the computer

For the final part, you are going to add code so that you can play multiple rounds of rock-paper-scissors against the computer. After each round of the game, you should print out the current scores using a function called printScores.

Be sure that all of the functions you write for this lab are well commented.

There’s no need to preserve your main function from the previous step, but much of what you wrote in the last step can be reused in this step if you find that helpful.

4.1. The printScores function

The printScores function should take the following parameters as input:

  • a string containing the user’s name

  • the number of games won by the user

  • the number of games won by the computer

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

4.2. The main function

The main function of your program should do the following:

  • Ask the user for their name and the number of wins needed to win the match. You can assume the user enters a valid positive integer for the number of wins needed.

  • Enable you to play multiple rounds of rock-paper-scissors against the computer until one player wins the match by winning a total of numWins rounds.

  • Make use of the getChoice function to get the human player’s choice each round and the calculateWinner function to compute the winner of each round of play.

  • Use the random.choice function to randomly choose "rock", "paper" or "scissors" for the computer’s move each round.

  • Display the result (a tie, or which player wins) after each round and then use the printScores function to show the current scores.

  • Print the winner of the match (the player that got to total number of wins first), and the total number of rounds that it took to finally win the match.

4.3. Sample output

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 rps.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
--------------------
... in 4 rounds.

$ python3 rps.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
--------------------
... in 4 rounds.

5. Answer the Questionnaire

Each lab will have 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.

Once you’re done with that, run handin21 again.

Turning in your labs…​.

Remember 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.