Problem set #2 -- due Wednesday 2/22
1. [Problem 5 from chapter 4 in the book (pp. 75-76)]
Given variables x and y, which refer to values 3 and 12.5, respectively, use
function print to print the following messages. When numbers appear in
the messages, variables x and y should be used.
a. The rabbit is 3.
b. The rabbit is 3 years old.
c. 12.5 is average.
d. 12.5 * 3
e. 12.5 * 3 is 37.5.
2. What is wrong with the following function:
def triangle_area(base, height):
return base*height/2.0
print("Base is: " + str(base))
3. The program below is supposed to calculate the length of the hypotenuse of a right
triangle with sides 3 and 4 and print out the answer. However, the program has three
errors in it: one syntax error and two runtime errors relating to type. What are the
three errors? Try to figure it out WITHOUT running the program.
# take the sqrt of a number
def my_sqrt(x):
return x ** (1//2)
# calculate the length of the hypotenuse of a right triangle
def hypotenuse_length(a, b)
sum = a**2 + b**2
return my_sqrt(sum)
print("If a is 3 and b is 4 then hypotenuse is " + hypotenuse_length(3,4))