| Author: | Christopher Haynes |
|---|---|
| Email: | chaynes@indiana.edu |
| Affiliation: | Indiana University |
| Course: | BL CSCI A201 |
| Date: | 2008-02-22 |
The missing line should be:def double(x):
"""
Return twice the value of x.
>>> double(1.2)
2.4
>>>
"""
#...
Answer: B
What's it print?def f():
print 'spam'
def g():
print 'eggs'
h = f
f = g
h()
Answer: A
What's it print?def f():
eggs = 'spam'
def g():
print eggs
f()
g()
Answer: C
What's it print?eggs = 'spam'
def f():
eggs = 'eggs'
f()
print eggs
Answer: A
Which of the following statements is a syntax error?Answer: B
[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
On a sheet of paper you can hand in write
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):
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
Sequential coding: write lots of code without a plan and without testing it until you hope you are done
Trial and error programming: try different thinks you think might work until something does work
Which of the following is a legal string?Answer: B
What does the following program print?s = 'a' s = s + s s = s + 'b' print s + 'c'
Answer: B
Which of the following can be in a Python expression?Answer: A
The value of 4/2 == 3 or 2 or 1 isAnswer: A
What does it print?
x = 3
def f(x):
x = 4
f(x)
print x
Answer: A
Another "what does it print?"
x = 3
def f():
x = 4
f()
print x
Answer: A
Yet another "what does it print?"i = 0
s = ''
while i < 5:
if i % 2:
s = s + str(i) + ' '
i = i + 1
print s
Answer: C
Have you tried to use the Python Syntax course web page?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)
running this program results in the following output:
2.4
Traceback (most recent call last):
File "C:\home\201\c\6\harmonic_mean.py", line 11, in -toplevel-
print harmonic_mean(3, 2)
File "C:\home\201\c\6\harmonic_mean.py", line 4, in harmonic_mean
return 2 / (inverse(x) + inverse(y))
ZeroDivisionError: integer division or modulo by zero
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