Week 8: More Top down design

Week 8 Goals

  • Learn about count and in operator on strings and lists

  • Learn to use Boolean flags

  • Practice Top Down Design

  • Practice writing function stubs

Get Week 8 In-class Code

To copy over the week 8 in-class example programs, do the following (If you have trouble with either of these steps, ask a Ninja or your professor for help):

  1. Create a w08-more-tdd subdirectory in your cs21/inclass directory, and cd into it:

    $ cd ~/cs21/inclass
    $ mkdir w08-more-tdd
    $ cd w08-more-tdd
    $ pwd
    /home/yourusername/cs21/inclass/w08-more-tdd
  2. Copy over the week 8 files into your w08-more-tdd subdirectory (check that they copied successfully copied by running ls:

    $ cp ~admin21/public/w08-more-tdd/* ./
    $ ls
    design-wheel.py  flags.py  strings_and_lists.py  w08-recap.py  wheel_puzzles.csv

Week 8 Code

  • strings_and_lists.py: Examples with strings and lists (in, count, and more)

  • flags.py: Examples using Boolean flags

A shorter TDD exercise

Wheel of fortune is a bit too complex for a short TDD exercise. Let’s do a simpler one.

We are going to walk through the process of designing a computer game to simulate the dice game Craps. The rules for a single game are as follows:

  1. A player rolls a pair of six-sided dice.

  2. If the initial roll in step 1 is a 2, 3, or 12, the player loses.

  3. If the initial roll in step 1 is a 7 or an 11, the player wins.

  4. Otherwise, the player must roll for point. In this case, the player keeps rolling until she re-rolls the initial roll in step 1. (a winning game), or rolls a 7 (a losing game).

What are the chances of winning a single game of craps? Instead of heading to the casino, let’s use our Computer Science top down design skills to see if this is a good game to play.

We are going to design a program that asks the user for the number of craps games to simulate, and then output the percentage of games won.

Working with a partner, think about how you can design main to be very short, maybe 5-10 lines with calls to 2-3 helper functions. How would you design main? What would the input parameters and return values of your helper functions be? Do not worry about the implementation of the helper functions at this point. Your discussion should focus on the high level concepts, not the low level python details.

You can make a new file craps_design.py and sketch out main and the helper functions stubs.