CSC 221: Introduction to Programming
Fall 2012

HW 2: Python Modules

This assignment will involve writing two small Python modules. These modules are similar to the examples from class (population estimation and reading level) in that they prompt the user for input values, perform calculations on those values, and display the results in a message. Your modules should follow the same basic style as the examples from class, with a comment block at the top documenting the purpose of the module, meaningful variable names, and readable I/O messages.

Note: this work must be entirely your own, with no outside assistance other than the instructor.

1. Don't Blink (blink.py)

The average person blinks once every four seconds (or 15 times per minute). You are to write a Python module that is given the blinking frequency for a person and then calculates the number of times that person would blink in a day. There should be two inputs to your code, the number of blinks observed and the time period during which those observations were made. For example, you might observe a person for 30 seconds and count 8 blinks during that period. The code should then extrapolate the blink rate over a full 24 hour period. For example, Enter the number of blinks observed: 8 Enter the duration of the observation (in seconds): 30 That extrapolates to 23040 blinks in a day.

Note that the number of blinks should be rounded to the nearest integer. There should also be a blank line between the input prompts and the output message.

2. Mortgage Payments (mortgage.py)

First-time home owners are often shocked to find out how much a mortgage really costs. You are to write a module that calculates the monthly mortgage payment for a given mortgage, and also the total amount paid over the term of that mortgage.

The formula for calculating the monthly mortgage payment is:

monthly payment =   loan • rate • (1+rate)months

(1+rate)months ‐ 1

where loan is the loan amount, months is the loan term (in months), and rate is the monthly interest rate (i.e., the yearly rate divided by 12).

Your code should prompt the user for the loan amount, the loan term (in years), and the yearly interest rate. It should calculate and display the monthly payment and the total amount that will be paid over the term of the loan. For example,

Enter the loan amount (in dollars): 200000 Enter the loan term (in years): 30 Enter the yearly interest rate (as a percentage): 4.5 Your monthly payment will be $1013.37 After 30 years, your payments will total $364813.2

The monthly payment should be rounded to the nearest penny. This can be accomplished by providing the round function with a second input, specifying the maximum number of digits. For example, round(1.2364, 2) evaluates to 1.24. There should be no spaces between a dollar sign and the associated amount, but there should be a blank line between the input prompts and the output message.