Practice Problems 1 Solutions

  1. [PP Problem 2.1] Indicate whether the result is an int or float by (not) including a decimal point, e.g. 5 for an int, 5.0 for a float.

    For each of the following expressions, what value will the expression evaluate to? The new operator we didn’t see in class is %, called modulus, which evaluates to the remainder of its left operand divided by its right operand.

    a. 6
    b. 20.0
    c. 4.5
    d. -4.5
    e. 4
    f. 1
    g. 1.0
    h. 1.0
    k. -5.0
    l. 19
    m. 35
    n. 7

  2. [PP Problem 4.5] Given variables x and y, which are assigned the values 3 and 12.5, respectively, use the print function to print the following messages (the print prints its input or arguments to the screen) by constructing the appropriate string. When numbers appear in the messages, variables x and y should be used instead.

    a. print("The rabbit is " + str(x) + ".")
    b. print("The rabbit is " + str(x) + " years old.")
    c. print(str(y) + " is average.")
    d. print(str(y) + " * " + str(x))
    e. print(str(y) + " * " + str(x) + " is " + str(x*y) + ".")

  3. If we execute the following code, what will the final value be for the variable z? # indicates a comment, everything on the line including and after the # is ignored by Python (is not executed).

    The final value for z is 49. While the expression i-1 and z+a are evaluated (and may print to the screen when executing in the shell), those expressions don’t change the values of i or z respectively (because there is no assignment operator). Because it is after the #, the statement z=2 is not executed at all. The only relevant statements are:

    i = 7
    a = i
    z = i * a
  4. 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, one logic error and one runtime error relating to type. What are the three errors? Try to figure it out without running the program. Think Python describes these three different types of errors (note they call “logic errors” “semantic errors”)

    # 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))
    1. Syntax error: Missing : after definition of hypotenuse_length
    2. Logic error: Use of // floor division in my_sqrt. This code effectively performs x ** 0. We want to use float division, i.e. 1/2.
    3. Runtime error: We can’t concatenate the float returned by hypotenuse_length to string in the print statement. We need to wrap hypotenuse_length with str().
  5. Write a function named x_intercept that has two parameters, the slope and intercept of a line, and returns the x intercept (i.e. the value of x where y=0).

    def x_intercept(slope, intercept):
        return -intercept / slope