1.9 Exercises
37
1.9 Exercises
Exercise 1.1: Error Messages
Save a copy of the program ball.py and confirm that the copy runs as the original.
You are now 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) Insert the word hello on the empty line above the assignment to v0.
b) Remove the # sign in front of the comment initial velocity.
c) Remove the = sign in the assignment to v0.
d) Change the reserved word print into pint.
e) Change the calculation of y to y = v0*t.
f) Change the line print(y) to print(x).
Filename: testing_ball.py.
Exercise 1.2: Volume of a Cube
Write a program that computes the volume V of a cube with sides of length L = 4
cm and prints the result to the screen. Both V and L should be defined as separate
variables in the program. Run the program and confirm that the correct result is
printed.
Hint See ball.py in the text.
Filename: cube_volume.py.
Exercise 1.3: Area and Circumference of a Circle
Write a program that computes both the circumference C and the area A of a circle
with radius r = 2 cm. Let the results be printed to the screen on a single line with an
appropriate text. The variables C, A and r should all be defined as separate variables
in the program. Run the program and confirm that the correct results are printed.
Filename: circumference_and_area.py.
Exercise 1.4: Volumes of Three Cubes
We are interested in the volume V of a cube with length L: V = L 3 , computed for
three different values of L.
a) In a program, use the linspace function to compute and print three values of L,
equally spaced on the interval [1, 3].
b) Carry out, by hand, the computation V = L 3 when L is an array with three
elements. That is, compute V for each value of L.
c) Modify the program in a), so that it prints out the result V of V = L**3 when L is
an array with three elements as computed by linspace. Compare the resulting
volumes with your hand calculations.
d) Make a plot of V versus L.
37
1.9 Exercises
Exercise 1.1: Error Messages
Save a copy of the program ball.py and confirm that the copy runs as the original.
You are now 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) Insert the word hello on the empty line above the assignment to v0.
b) Remove the # sign in front of the comment initial velocity.
c) Remove the = sign in the assignment to v0.
d) Change the reserved word print into pint.
e) Change the calculation of y to y = v0*t.
f) Change the line print(y) to print(x).
Filename: testing_ball.py.
Exercise 1.2: Volume of a Cube
Write a program that computes the volume V of a cube with sides of length L = 4
cm and prints the result to the screen. Both V and L should be defined as separate
variables in the program. Run the program and confirm that the correct result is
printed.
Hint See ball.py in the text.
Filename: cube_volume.py.
Exercise 1.3: Area and Circumference of a Circle
Write a program that computes both the circumference C and the area A of a circle
with radius r = 2 cm. Let the results be printed to the screen on a single line with an
appropriate text. The variables C, A and r should all be defined as separate variables
in the program. Run the program and confirm that the correct results are printed.
Filename: circumference_and_area.py.
Exercise 1.4: Volumes of Three Cubes
We are interested in the volume V of a cube with length L: V = L 3 , computed for
three different values of L.
a) In a program, use the linspace function to compute and print three values of L,
equally spaced on the interval [1, 3].
b) Carry out, by hand, the computation V = L 3 when L is an array with three
elements. That is, compute V for each value of L.
c) Modify the program in a), so that it prints out the result V of V = L**3 when L is
an array with three elements as computed by linspace. Compare the resulting
volumes with your hand calculations.
d) Make a plot of V versus L.
