Session 9

Opening Exercise: Growing the Odds

What does this program print?

```    for i in range(10):
print(i+1)
```

Let's modify this program incrementally to (by demo)...

• Make it print only the even numbers.

• Make it print them on a single line.

• Make it print all the even numbers on a single line, for every even number.

That third one is complex enough to use some of the ideas you read in Chapter 3 about designing algorithms and developing programs. We need to think about what we have, and what we would like it to be.

One technique is almost always useful: Break the problem into smaller steps. We have a program that prints the even numbers up to a limit.

• We want to do this only if the limit is even. (v3a)
• Make the current program the suite of a choice.
• Choose to do it only if the limit is even.

• Now, we want to repeat this for all numbers up to 10. (v3b)
• Make the current program the suite of a loop.
• Make the loop change the old program's variable.

This program is more complex than many of us want to write from scratch:

```    for counter in range(10):
n = counter + 1
if n % 2 == 0:
result = ''
for i in range(n):
value = i + 1
if value % 2 == 0:
result += (str(value) + ' ')
print(result)
```

But by breaking complex steps down into smaller, more manageable steps, we were able to design a solution. That process also enabled us to grow the program slowly and have a working solution at each point along the way.

With practice, you can do the same.

(Try to think this way on Homework 4.)

Homework 3 Review, Part 1

Debug these programs.

• Example 1. Double =. No classification.
Issues: 100*100.

• Example 2. 24.9.
Issues: Line wrap. Use of elif.

A look at my solution.

Notes.

• Make your program match the knowledge in the problem.

There is no 10000 in the specification. Do you know why it's right? Why 100*100? Not all formulas can be adjusted so easily after the fact, and every adjustment makes the reader slow down and work harder than she needs.

This is similar to not pre-compiling values in your program.

The number and order of inputs. 6'-6" versus 78".

Homework 3 Review, Part 2

Debug these programs.

• Example 1. time is a string. () on the formula. Order of inputs.
Issues: Too complex too soon with if. Sidesteps the assignment with complex formula.

• Example 2. Order of inputs again. Resets total each time it enters loop. year -- hmmm... rate needs to be divided by 100.
Issues: Whitespace. Name apr. Same computation twice. (Simpler!)

A look at my solution.

Notes.

• Understand the tools you use.

Do you understand the compound interest formula? In any case, it is overkill here. We need yearly balance updates, not just the final answer.

No while or for loop? Something may be amiss... The purpose of the assignment was to give you practice with a loop. (It also helps you understand how the compound interest formula works!)

Order of inputs. Filenames.

Grader as user. Automated testing (show).

More general notes.

• Test your program after every change.

Even if you add only a print statement. We can break a working program.

• Take care of appearances.

In your code: whitespace, names, comments line wrap. In your user interaction: input prompts (space) and print labels.

A big one: Do your own work. Sharing. Finding solutions.

Wrap Up

• Code -- today's zip file

• Reading -- Re-read Chapter 3, pages 153-178. Try the authors' suggestions as you do your programming assignment.

• Homework -- Homework 4 is available and is due next session.

Eugene Wallingford ..... wallingf@cs.uni.edu ..... September 23, 2014