Which of the following belongs in a function’s docstring? List all that apply.
Consider the following function:
def print_squares(n):
""" docstring missing! help! """
= 0
i while i < n:
+= 1
i print(i, i**2)
return i**2
Which of the following should be included in the docstring? List all that apply.
The function prints the square of each number from 1 through
n
The function uses a while
loop
The function returns the square of n
The function returns the square of i
The function uses a counter variable called
i
Even with the correct subset of the above, the function could result in an error. Write a precondition that can be included such that as long as the precondition is satisfied, the function cannot cause an error.
Consider the following program, noticing that several points in the
code are marked with comments (e.g., M1
).
Important: the markers refer to the lines they’re
on, not the lines following them.
# M1
def a(v1, v2):
# M2
= v1 + v2
v3 # M3
print(v3)
# M4
4, 6)
a(# M5
M1
through M5
) is
v2
in scope? List all that apply.M1
through M5
) is
v3
in scope? List all that apply.Consider the following program:
def print_rectangle_area(width, height):
""" Print the area of a width-by-height
rectangle. Pre: width and height are numbers. """
= width * height
area print(area)
= 4
w = 3
h = w * h
a print_rectangle_area(w, h)
print(h*w)
print(width * height)
print(w * h)
print_rectangle_area(h, w)
What does the following program print?
def f(x):
3 * x)
g(
def g(x):
print(x + 2)
4) f(
What does the following program print?
= 4
x
def f(x):
return 3 * x
def g(x):
return x + 2
print(f(g(x)))
print(g(f(x)))