Homework 1

Due: Friday, January 18, 2014 at 11:59pm
Points: 100


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

Here, what the computer prints is in regular typewriter font, what you type is in italic typewriter font, and the symbol “↵” represents a return or enter.

  1. (25 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
    formula to compute vapor pressure from temperature
    Here, e is the base of the natural logarithms; use the math module’s function math.exp(x) to compute ex. To do this, put the line ``import math'' at the beginning of your program.

    Use a function to compute the approximation. Your program should print the output in exactly this form (note you must have exactly 2 digits after the decimal point):

    Enter temperature in degrees C: 10
    At this temperature, the vapor pressure is approximately 12.27 millibars
    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: One way to print the vapor pressure is to use the format string “"%6.2f" % vp(t)”, where t is the temperature and vp(t)> is the function to compute the vapor pressure from the temperature t.

  2. (25 points) Modify the program you wrote for question 1 to print the estimated vapor pressures for temperatures between −20 and 50 degrees Celsius at 5 degree intervals. Your output should look like this:
    temp   pressure
    ----   --------
    -20       1.26
     …
     50     124.02
    with the proper numbers substituted for the “…”s. Again, only compute the vapor pressure to 2 decimal places. Call this program “vptable.py”.

  3. (25 points) Write a program that reads an integer n and draws a regular polygon with n sides.

    Call this program “poly.py”.

    Hint: Remember that a “regular polygon" is one that has sides of equal length, and all angles of equal size.

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

    Here is the program:

    ftemp = 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.


You can also obtain a PDF version of this. Version of January 4, 2019 at 12:06PM