In Class: Week 8 Tuesday
More Top-Down Design
Run update21:
$ update21
$ cd cs21/inclass/w08-search
$ ls
gradebook.txt grades_list_soln.py testsearches.py
grades_list.py searchCards.py
Topics
- list of lists
- Iterative refinement
- Bottom-up implementation
- Unit testing using python interactive mode
- Search
List of Lists
Lists store a sequence of any type. We have seen how we can store a list of strings,
ints, and objects. We can also store lists in our list. Doing so requires that we
closely examine we access and modify the list.
- In w08/grades_list.py, you will see a program similar to last week's quizGrade.py example. Here, though, we want to accomplish three goals:
- Instead of throwing away each line after using it, we want to store the contents (in a list)
- We will create a function that can access a student's quiz scores and display them in
printStudentScores()
- Then, we will create a function that prints out each student's name and quiz
score for a particular quiz in printQuizScores.
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 Blackjack. Open up
blackjack_tdd1.py in your w07-design folder to review our design.
Note that I have added
a deck of cards to our design to make the problem more realistic
- To begin, we will analyze our first level of design and see which tasks
need to be further broken down. This process of breaking subproblems into
a set of even smaller subproblems is known as iterative refinement.
Specifically, we will see how playUser() requires a function for
flipping a single card as well as a function for getting a yes-or-no response
from the user.
- Note that importance of prototyping our functions. It is import
to be specific and document the purpose of each function as well as the
parameters and return values so we clearly indicate how the function
should be used.
- Together, we will begin implementation on the bottom-level functions.
First, we will look at my implementation of generateDeck(). We will
use the python interpretor to perform unit testing to notice
that my implementation was no complete.
- On your own, work on implementing the getYesOrNo() function.
When finished, let's try some unit testing on getYesOrNo()
(see below).
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"); ?>