In Class: Week 7
If you have not already done so, run update21:
$ update21
$ cd ~/cs21/inclass/w07-design
Topics
- Nested for loops
- File Input/Output
- Top-down design
Nested for loops
Real-world programs involve composing multiple patterns together
to solve a problem. We have seen how if statements can be used
inside for loops. But we can also have loops inside of loops.
- First, we will work in nestedFor.py. Together, we will
use nested for loops to print out a square box of numbers. For example,
if a user enters 5 for the value of n, your program
should output the numbers 1 through n for each line, for a total
of n lines:
12345
12345
12345
12345
12345
- Next, modify the loop to instead print out a triangle of numbers. The
key is to understand how the inner-for loop interacts with the outer-for
loop
1
12
123
1234
12345
- In drawGrid.py, you will see another example of nested-for
loops that displays a 2-dimensional grid of circles.
File Input/Output
Another property of real-world applications is that data is retrieved from
files - persistent storage of information - as opposed to user input. We
will work with text files which can be abstracted as a series of strings,
one string per line of the file.
- In shopping.py, I have given you two example ways to read from
files - either by iterating over the file using a for loop (the function
readAndPrint(filename)) or by using readLines to obtain
a list of all lines (function readAndGetList). If we only
need to use a line one time, the for loop is often used. If, however, we
need to keep track and reexamine the file, a list of lines is more useful.
Finally, we will create a new file by asking the user for shopping ingredients and outputting them to a new file.
- Examine both of the files from the previous example:
$ cat cupboard.txt
flour
sugar
chocolate chips
coloring
potato chips
apple sauce
$ cat shoppingList.txt
cereal
beer
wine
- For a more advanced use of file input, we will explore quizGrades.py which will not only read from the file gradebook.txt, but
parse its contents. One line of a file is read as a string, but we will
need to extract multiple pieces of information from that string including
the name (string) and several quiz grades (as ints)
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
Top-down design is a lot like creating a paper outline for a large paper
where you
start with the main sections, and iteratively refine each part with
more and more detail until you are ready to start writing the paper.
When you use top-down design, your resulting program's structure
should closely match that of the steps: the main function should have
calls to a few high-level functions, usually one for each high-level step;
high-level functions have calls to functions that implement sub-steps of the
high-level step; and so on.
Iterative Refinement
When writing a large program, programmers use Iterative Refinement:
do some top-down design, write function stubs for this part of code and maybe
some implementation, then test. Iteratively, add more functions, and
perhaps refine some of the steps using Top-Down design, and test, and so on.
The idea is to write some code, test it, then write a little more code,
and test it before writing even more. Usually, I write a function, then
test it, write another function, test it, ... This way if I'm careful
about testing, I know that if there is a bug in my program it is with
the new code I've just added.
Function Prototyping
We often use prototyping to just put in function stubs
so we can test the whole program's flow of control without having to have
a full implementation. For example, here is a stub for a function to
compute square root (it doesn't actually do anything but print out
a message with the parameter value and return some bogus value, but
I can call it from other parts of my program):
def squareRoot(num):
"""
This function computes the square root of a number.
num: the number
returns: the square root of num
"""
print "inside squareRoot num is", num
# TODO: implement this function
return 1 # a bogus return value, but it let's me run the program
# and make calls to this function stub to "see" program flow
Let's try it out...
We are going to walk through the process of designing a computer game to
play Black Jack. Follow along and work with a partner or two using
your worksheets.
include("style/footer.php"); ?>