98
4 Functions and the Writing of Code
b) Type in the code and run the program to confirm that your predictions are correct.
Filename: read_code_1.py.
Exercise 4.3: Reading Code 2
a) Read the following code and predict the printout produced when the program is
executed.
def f(x):
if x < 2:
return 0
else:
return 2*x
x = 0
for i in range(0, 4, 1):
x += i
print(x)
for i in range(0, 4, 1):
x += i*i
print(x)
for i in range(0, 4, 1):
print(f(3*i-1))
b) Type in the code and run the program to confirm that your predictions are correct.
Filename: read_code_2.py.
Exercise 4.4: Functions for Circumference and Area of a Circle
Write a program that takes a circle radius r as input from the user and then computes
the circumference C and area A of the circle. Implement the computations of C and
A as two separate functions that each takes r as input parameter. Print C and A to the
screen along with an appropriate text. Run the program with r = 1 and confirm that
you get the right answer.
Filename: functions_circumference_area.py.
Exercise 4.5: Function for Adding Vectors
Write a function add_vectors that takes 2 vectors (arrays with one index are often
called vectors) a and b as input arguments and returns the vector c, where c = a +b.
Place the function in a little program that calls the function and prints the result.
The function should check that a and b have the same length. If not, None should be
returned. Confirm that your function works by comparing to hand calculations (i.e.,
just choose some arrays and test).
Filename: add_vectors.py.
4 Functions and the Writing of Code
b) Type in the code and run the program to confirm that your predictions are correct.
Filename: read_code_1.py.
Exercise 4.3: Reading Code 2
a) Read the following code and predict the printout produced when the program is
executed.
def f(x):
if x < 2:
return 0
else:
return 2*x
x = 0
for i in range(0, 4, 1):
x += i
print(x)
for i in range(0, 4, 1):
x += i*i
print(x)
for i in range(0, 4, 1):
print(f(3*i-1))
b) Type in the code and run the program to confirm that your predictions are correct.
Filename: read_code_2.py.
Exercise 4.4: Functions for Circumference and Area of a Circle
Write a program that takes a circle radius r as input from the user and then computes
the circumference C and area A of the circle. Implement the computations of C and
A as two separate functions that each takes r as input parameter. Print C and A to the
screen along with an appropriate text. Run the program with r = 1 and confirm that
you get the right answer.
Filename: functions_circumference_area.py.
Exercise 4.5: Function for Adding Vectors
Write a function add_vectors that takes 2 vectors (arrays with one index are often
called vectors) a and b as input arguments and returns the vector c, where c = a +b.
Place the function in a little program that calls the function and prints the result.
The function should check that a and b have the same length. If not, None should be
returned. Confirm that your function works by comparing to hand calculations (i.e.,
just choose some arrays and test).
Filename: add_vectors.py.
