# CS21 Lab 4: while loops and functions

Due Saturday (February 20) before midnight

As always, make sure your programs are in the correct directory (cs21/labs/04). We will only grade files submitted by handin21 in this directory.

Programming tips

As your programs become larger, it is even more important to develop good habits:

• Use a comment at the top of the file to describe the purpose of the program.
• Use variable names that describe the contents of the variables.
• Use def main() to combine the steps of your program into a function.
• Each function you write needs a triple-quoted comment just under the def function(): line, describing what the function does.

The above are mandatory and you will be graded on them.

We strongly advise you to write your programs incrementally and test them as you go. No one writes interesting programs all at once. As you write, test that your program does what you expect (even if it doesn't yet do what you want). If it surprises you, make sure you understand what's happening before writing more code.

If we have provided sample input and output, make sure your program matches the expected behavior! You should also test your program using inputs and outputs that are not shown here; after all, we will. Come up with your own test cases and verify that the program is producing the right output on them.

In this lab we will use python's random module. Remember, to import the random module, add this above any functions you define:

from random import *


To generate a random integer between a and b, use randrange(a,b+1).
To randomly select an item from a list, use choice(L).

rnum = randrange(1,11)           # generate random number between 1 and 10

L = ["jeff","lisa","joshua"]
rname = choice(L)                # pick item from list L


Lab Session tips

If you are in lab and waiting for help, see our getting-the-most-out-of-lab doc. Even if you have no idea why your program isn't working, there are always things you can do to help debug the program and make progress.

1. Candy Hearts

Write a function called candyheart(), in a file named hearts.py, that returns a candy heart message (a string). In your function you should create a list of 5-10 candy heart messages (see below), and then randomly choose one of the messages and return it. NOTE: to get the messages to "fit" on the candy hearts, they usually have one word on each line. You can easily do this by putting the newline character in your message (e.g., "WINK\nWINK").

In the main function of your hearts.py program, use a simple for loop to call the candyheart() function 5 times, and print out the returned string.

Here is a sample run of the program.

$python hearts.py WINK WINK ALL MINE MY BABY LOVE YOU! LOVE YOU!  #### Extra Challenge Note: extra challenges are just for fun (i.e., no bonus points). Please only attempt them after completing your regular assignment. Add some extra "ASCII-art" to the string returned by candyheart(). $ python hearts.py

          
      
  
$$$ALL MINE$$$

$BE$
$TRUE$



$ 2. Horse Racing Using random numbers, we can simulate a simple horse race. Imagine you have 4 horses, and they are all racing toward the finish line. We can show the progress of the race using a repeating bar graph: 1: 2: 3: 4: --------------------------------------------------------------------------- 1:HH 2:H 3:HHHHH 4:H --------------------------------------------------------------------------- 1:HHHHHHH 2:HHHHH 3:HHHHHH 4:HHHHHH ---------------------------------------------------------------------------  In the three "timesteps" shown above, horse #3 gets off to a fast start, but horse #1 quickly catches up. Write a program called horserace.py that simulates a 4-horse race. During each "timestep" of the race, your program should: • for each horse, choose a random number from 1 to 5, and "move" the horse • also for each horse, check to see if they have crossed the finish line, thus ending the race • output the current status of the race Hint: note the use of "for each horse" above. YES, you should use a python list to store the horse info, and a "for" loop to update the horses. You will also need a "while" loop to handle the simulated race. In the race shown here, my finish line is 75 characters from the start. Here is how the end of the above race might look: 1:HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH 2:HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH 3:HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH 4:HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH --------------------------------------------------------------------------- 1:HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH 2:HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH 3:HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH 4:HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH --------------------------------------------------------------------------- 1:HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH 2:HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH 3:HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH 4:HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH ---------------------------------------------------------------------------  And here's how the whole thing might look: #### Extra Challenge Once the race is over, determine and display which horse won the race. 3. Two-Dice Pig Two-Dice Pig is a variation of the dice game Pig. We are going to write a simplified one-player version of two-dice pig. In our version, two dice are rolled over and over until a total score of 100 is reached. Based on the results of rolling the two dice, the scoring each time is as follows: • if neither is a 1, their sum is added to the total score • if a single 1 is rolled, nothing is added to the total score • if both are 1, the total score is reset to zero Here is how a single run of the program might look: $ python twodice.py
1.  2  1  score = 0
2.  5  6  score = 11
3.  4  3  score = 18
4.  1  1  score = 0
5.  4  6  score = 10
6.  2  2  score = 14
7.  6  5  score = 25
8.  6  6  score = 37
9.  6  4  score = 47
10.  3  4  score = 54
11.  6  1  score = 54
12.  4  4  score = 62
13.  3  1  score = 62
14.  4  6  score = 72
15.  4  3  score = 79
16.  4  6  score = 89
17.  6  5  score = 100


For this program, we want you to practice writing functions, so please follow the directions below:

#### Part A: write the roll() function

In a file called twodice.py, first write a roll() function that simulates rolling two 6-sided dice. Your function should return a python list that contains the results of rolling the two dice.

Add a main() function that uses a simple for loop to both call the roll() function and print the results, ten times.

Here's an example (please format the output as shown below):

$python twodice.py 1. 1 5 2. 6 4 3. 6 5 4. 6 2 5. 3 4 6. 5 4 7. 1 5 8. 3 3 9. 5 4 10. 1 2  #### Part B: add the score(L) function Once the above is working, add a score(L) function that has one parameter, a python list. This is the same list that was returned from the roll() function earlier. This function should calculate and return the score of the rolled dice (see scoring rules above). In your for loop in main(), add in the call to score() and display the results. Note: in the output below, the score shown is the total game score, not the value returned by the score() function. You will need to add this total game score to your main() function, as well. You will also need to come up with a way to reset this total game score to zero when double ones are rolled! $ python twodice.py
1.  5  5  score = 10
2.  6  2  score = 18
3.  2  4  score = 24
4.  5  6  score = 35
5.  2  6  score = 43
6.  1  4  score = 43
7.  4  5  score = 52
8.  1  1  score = 0
9.  3  6  score = 9
10.  1  1  score = 0


Run the above code a few times to make sure it works for all cases!

#### Part C: add a while loop

Once you have the above working, change the for loop in main() to a while loop. Have your program keep rolling until it reaches a total game score of 100 (or more).

Here's an example of the full program:

\$ python twodice.py
1.  3  1  score = 0
2.  6  3  score = 9
3.  1  2  score = 9
4.  6  6  score = 21
5.  1  3  score = 21
6.  5  6  score = 32
7.  6  4  score = 42
8.  1  1  score = 0
9.  6  3  score = 9
10.  1  4  score = 9
11.  2  2  score = 13
12.  3  1  score = 13
13.  2  1  score = 13
14.  6  2  score = 21
15.  2  1  score = 21
16.  1  4  score = 21
17.  1  6  score = 21
18.  4  6  score = 31
19.  2  4  score = 37
20.  3  1  score = 37
21.  1  3  score = 37
22.  6  5  score = 48
23.  5  2  score = 55
24.  3  1  score = 55
25.  3  1  score = 55
26.  6  3  score = 64
27.  3  1  score = 64
28.  4  2  score = 70
29.  2  4  score = 76
30.  3  1  score = 76
31.  6  4  score = 86
32.  3  6  score = 95
33.  1  2  score = 95
34.  4  1  score = 95
35.  3  1  score = 95
36.  5  4  score = 104


#### Extra Challenge

Change your program to run the game many times (1000?), and calculate the average number of rolls needed to reach a total score of 100.

How would the average number of rolls change as we increase the required total game score (200, 300, 400, ...)?

Submit

Don't forget to run handin21!