In Class: Week 8 Tuesday:
Top-Down Design Practice
Create a week08 subdirectory in your
cs21/class directory by running update21:
$ update21
$ cd
$ cd cs21/class/week08
Topics
- Mid-term survey comments
- Lab 6 common problems
- Continue top-down design
- Bottom-up implementation
- Unit testing using python interactive mode
Top Down Design
Top Down Design is a problem solving technique where:
- Start with General Description of Problem
- Break it into several high-level steps
- Iteratively break the steps into smaller steps until you
have steps that are easy to solve
In-class work
- Last week, we started working on the program to play Black Jack. Open up
blackjack.py to review our design.
- Together, we will begin implementation on the bottom-level functions. I
will implement getYesOrNo. When I'm finished, let's try some unit testing on
getYesOrNo() (see below).
- Once we are satisfied with getYesOrNo(), work individually on implementing
other functions in blackjack.py. Start with printInstructions()
and getName(). Perform unit testing on both of these functions to see
if they work as you expect.
- Next, work on implementing flipCard(). This function will require
the use of the random library, so be sure to import random:
from random import *
Perform unit-testing on flipCard() multiple times to see if you get
numbers that you expect.
- If you finish flipCard(), move up a level in implementation and work
on playUser(). Since flipCard is implemented and tested, you can concentrate
on the larger problem of executing one user turn.
Unit Testing
Unit testing is the process of validating functions as individual pieces,
in isolation from other large portions of the program. This allows a developer
to focus solely on the (small) subproblem at hand and think carefully
about potential errors. To perform unit testing:
- You can write calls to getYesOrNo from main() and run your program
to see the results
- You can also write a special testing function, called testGetYesOrNo()
whose sole purpose is to call the function multiple times with different
arguments (if applicable).
- In python, you can open up python in interactive mode and make calls
to the function getYesOrNo().
To use the python interpretor, add the following lines of code to the bottom
of your program where main() is:
if __name__ = "__main__":
main()
This allows python to import your program without running it. To do so, run
python in interactive mode:
$ python
In interactive mode, use the command import to load your file.
>>>import blackjack
To test a function, say flipCard(), you can invoke your method:
>>>blackjack.flipCard()
in function flipCard()
5
The function flipCard was invoked. Any print statements are output to the
screen. Finally, python will automatically output the return value, which
was 5 in this case.
If you make changes to your program, you can reload your file in
the python interpretor by calling the reload command:
>>>reload(blackjack)
Lastly, you can see your function prototypes and block comments using the help
command:
>>>help(blackjack)
include("style/footer.php"); ?>