Homework #1

Due: Thursday, January 23, 2018 at 11:59 p.m.
Points: 100


Please turn in your answers for the homework assignment on Canvas under Homework #1 in Assignments.

  1. (30 points) Start up an interactive Python session and try typing in each of the following commands. Write down the results of each.
    1. print "What ever you do"
    2. print "What" "ever" "you" "do"
    3. print "What", "ever", "you", "do"
    4. print 3
    5. print 3 + 4
    6. print 3 + 2 * 2
    7. print (3 + 2) * 2
    8. print 3 + (2 * 2)
    9. print 3 + 2
    10. print "3" + "2"
    11. print "3 + 2 = ", 3 + 2
    12. print 3 * 2
    13. print 3 ** 2
    14. print 3 / 2
    15. print 3.0 / 2.0
  2. (30 points) Write a program that asks the user for the temperature t in degrees Celsius and then displays the estimated vapor pressure of water vapor in millibars at that temperature using the approximation
    Here, e is the base of the natural logarithms; use the math module’s function math.exp(x) to compute ex.

    Your program should print the output in exactly this form (you may change the number of digits after the decimal point, but you must have at least two):

    Enter temperature in degrees C: 10
    At this temperature, the vapor pressure is approximately 12.2716959939 millibars
    Here, what the computer prints is in regular font, what you type is in italic font, and the symbol “↵” represents a return or enter. Use your program to estimate the temperature at which the vapor pressure is approximately 10 millibars (to two decimal places).

    Call this program “vp.py”, and put your answer in a comment at the beginning of the program.

    Hint: To use the math module, put the line import math at the beginning of your program

  3. (40 points) The goal of the following program is to convert a temperature from degrees Farenheit to degrees Celsius. The formula is:
    where F is the number of degrees Fahrenheit and C the number of degrees Celsius.

    Here is the program:

    ftemp = raw_input("Enter degrees in Fahrenheit: ")
    ctemp = 5 / 9 * (ftemp - 32)
    print ftemp, "degrees Fahrenheit is", ctemp, "degrees centigrade"
    But there are two problems:
    1. Whenever I run the program, it gives me a TypeError message as soon as I enter my number.
    2. Once I fixed this problem, it always tells me the result is −0.0 degrees Celsius.

    Please fix both these problems, so the program converts Fahrenheit to Celsius correctly. The program must handle floating point numbers, so entering “32.5” should produce a (small) real number, not a ValueError.

    Call your fixed program “ftoc.py”, and explain what caused the two problems in a comment at the beginning of the program.


Matt Bishop
Department of Computer Science
University of California at Davis
Davis, CA 95616-8562 USA
Last modified: January 6, 2018 at 7:48PM
Winter Quarter 2018
You can get a PDF version of this