4.3 Exercises
97
4.3 Exercises
Exercise 4.1: Errors with Colon, Indent, etc.
Write the program ball_function.py as given in the text and confirm that the
program runs correctly. Then save a copy of the program and use that program
during the following error testing.
You are supposed to introduce errors in the code, one by one. For each error introduced, save and run the program, and comment how well Python’s response corresponds to the actual error. When you are finished with one error, re-set the program
to correct behavior (and check that it works!) before moving on to the next error.
a) Change the first line from def y(v0, t): to def y(v0, t), i.e., remove the
colon.
b) Remove the indent in front of the statement g = 9.81 inside the function y, i.e.,
shift the text four spaces to the left.
c) Now let the statement g = 9.81 inside the function y have an indent of three
spaces (while the remaining two lines of the function have four).
d) Remove the left parenthesis in the first statement def y(v0, t):
e) Change the first line of the function definition from def y(v0, t): to def
y(v0):, i.e., remove the parameter t (and the comma).
f) Change the first occurrence of the command print(y(v0, time)) to
print(y(v0)).
Filename: errors_colon_indent_etc.py.
Exercise 4.2: Reading Code 1
a) Read the following code and predict the printout produced when the program is
executed.
def f(x):
return x**2
def g(x):
return 2*x
for x in [1, 5, 6, 9]:
if x < 5:
# case 1
print(f(x))
elif x == 5:
# case 2
print(2*f(x))
elif x > 5 and x < 8:
# case 3
print(f(x+4) + g(x*2) - g(2))
else:
# case 4
y = x + 2
print(g(y))
97
4.3 Exercises
Exercise 4.1: Errors with Colon, Indent, etc.
Write the program ball_function.py as given in the text and confirm that the
program runs correctly. Then save a copy of the program and use that program
during the following error testing.
You are supposed to introduce errors in the code, one by one. For each error introduced, save and run the program, and comment how well Python’s response corresponds to the actual error. When you are finished with one error, re-set the program
to correct behavior (and check that it works!) before moving on to the next error.
a) Change the first line from def y(v0, t): to def y(v0, t), i.e., remove the
colon.
b) Remove the indent in front of the statement g = 9.81 inside the function y, i.e.,
shift the text four spaces to the left.
c) Now let the statement g = 9.81 inside the function y have an indent of three
spaces (while the remaining two lines of the function have four).
d) Remove the left parenthesis in the first statement def y(v0, t):
e) Change the first line of the function definition from def y(v0, t): to def
y(v0):, i.e., remove the parameter t (and the comma).
f) Change the first occurrence of the command print(y(v0, time)) to
print(y(v0)).
Filename: errors_colon_indent_etc.py.
Exercise 4.2: Reading Code 1
a) Read the following code and predict the printout produced when the program is
executed.
def f(x):
return x**2
def g(x):
return 2*x
for x in [1, 5, 6, 9]:
if x < 5:
# case 1
print(f(x))
elif x == 5:
# case 2
print(2*f(x))
elif x > 5 and x < 8:
# case 3
print(f(x+4) + g(x*2) - g(2))
else:
# case 4
y = x + 2
print(g(y))
