Week 7

Review, Program Design, and Debugging

Author:Christopher Haynes
Email:chaynes@indiana.edu
Affiliation:Indiana University
Course:BL CSCI A201
Date:2008-02-22
Copyright © 2008, Christopher Haynes—all rights reserved.

clicker The missing line should be:

def double(x):
    """
    Return twice the value of x.
    >>> double(1.2)
    2.4
    >>>
    """
    #...
  1. return 2.4
  2. return 2 * x
  3. return "2 * x"

Answer: B

clicker What's it print?

def f():
    print 'spam'
def g():
    print 'eggs'
h = f
f = g
h()
  1. spam
  2. eggs
  3. nothing, there is an error

Answer: A

clicker What's it print?

def f():
    eggs = 'spam'
def g():
    print eggs
f()
g()
  1. spam
  2. eggs
  3. nothing, there is an error

Answer: C

clicker What's it print?

eggs = 'spam'
def f():
    eggs = 'eggs'
f()
print eggs
  1. spam
  2. eggs
  3. nothing, there is an error

Answer: A

clicker Which of the following statements is a syntax error?

  1. pram = 'spam'
  2. "spam" = ham
  3. "spam = ham"
  4. B and C
  5. all of the above

Answer: B

Lewis Carroll Quotation

[The White Knight said:] The name of the song is called "Haddocks' Eyes".'

'Oh, that's the name of the song, is it?' Alice said, trying to feel interested.

'No, you don't understand,' the Knight said, looking a little vexed. 'That's what the name is called. The name really is "The Aged Aged Man".'

'Then I ought to have said "That's what the song is called"?' Alice corrected herself.

'No, you oughtn't: that's quite another thing! The song is called "Ways and Means": but that's only what it's called, you know!'

'Well, what is the song, then?' said Alice, who was by this time completely bewildered.

'I was coming to that,' the Knight said. 'The song really is "A-sitting On a Gate": and the tune's my own invention.'

Through the Looking Glass, chapter 8

Distinctions in the quotation

Some reasons why these distinctions matter

Crystal / Mud

On a sheet of paper you can hand in write

Constants

Program element order

It is good style to order elements of your program file as follows (not all elements may be present in a program, and additional comments may be placed anywhere they are a help):

  1. Initial comment as described in point 1 above
  2. Import statements
  3. Constant declarations
  4. Global variable declarations
  5. Function definitions
    • constants, imports, and variables that are only used by one function may be placed just before the function
  6. Statements that execute the program
    • often just a single call to a function named main

Any order is possible, as long as variables are assigned before they are referenced

  • recall that function bodies are not executed when the function is defined

Good program development strategy

Another program development strategy

Combining top-down and bottom-up design

One way not to develop programs

Sequential coding: write lots of code without a plan and without testing it until you hope you are done

Another way not to develop programs

Trial and error programming: try different thinks you think might work until something does work

clicker What does the following program print?

s = 'a'
s = s + s
s = s + 'b'
print s + 'c'
  1. 'aaabc'
  2. 'aabc'
  3. 'abc'
  4. 'ac'
  5. nothing: there is an error

Answer: B

clicker Which of the following can be in a Python expression?

  1. or
  2. if
  3. return
  4. while
  5. A and B
  6. A, B and C
  7. all of the above
  8. none of the above

Answer: A

clicker The value of 4/2 == 3 or 2 or 1 is

  1. a true value
  2. a false value
  3. niether a true or false value
  4. there is a syntax error

Answer: A

clicker What does it print?

x = 3
def f(x):
    x = 4
f(x)
print x
  1. 3
  2. 4
  3. nothing: there is a syntax error
  4. nothing: there is a runtime error

Answer: A

clicker Another "what does it print?"

x = 3
def f():
    x = 4
f()
print x
  1. 3
  2. 4
  3. nothing: there is a syntax error
  4. nothing: there is a runtime error

Answer: A

clicker Yet another "what does it print?"

i = 0
s = ''
while i < 5:
    if i % 2:
        s = s + str(i) + ' '
    i = i + 1
print s
  1. 1
  2. 1 2
  3. 1 3
  4. 1 2 3
  5. 1 3 5

Answer: C

clicker Have you tried to use the Python Syntax course web page?

  1. yes, and it is helpful
  2. yes, I understand it but I don't find it helpful
  3. yes, but I don't understand how to use it
  4. no, but I've been meaning to
  5. no, and I don't want to

Functional composition example

The following program has a function harmonic_mean that computes the mathematical harmonic mean of two numbers

# harmonic_mean.py, by chaynes@indiana.edu

def harmonic_mean(x, y):
    return 2 / (inverse(x) + inverse(y))

def inverse(x):
    return 1 / x

print harmonic_mean(3.0, 2.0)

print harmonic_mean(3, 2)

Function composition

Example: countdown

def countdown(n):
    """
    Print a line with the integers n, n-1, ..., 1, separated by spaces.
    >>> countdown(5)
    >>>
    """
    s = ''
    while n >= 1:
        s = s + str(n)
        if n != 1:
            s = s + ' '
        n = n - 1
    print s

Python input/output