Homework Assignment 3

Basic Control of Flow


CS 1510
Introduction to Computing
Fall Semester 2014


Due: Thursday, September 18, at 10:00 AM


Introduction

This assignment gives you a chance to move beyond the limits of the IPO pattern and write programs that make choices and repeat actions.

Feel free to use all of the things that you are learning to create a clear, readable program. However, be sure not to change the input/output specification. In particular, be sure that your program asks for and accepts the same number of data values specified in the requirements, and in the order specified.

As always, use the Python shell to experiment and to build the expressions you need to compute the desired results.

Be sure to check out the course programming standards. (Most Homework 2 submissions failed to comply with one simple requirement...)



Spec 1: Body Mass Index

According to the Centers for Disease Control, the Body Mass Index (BMI) is a reliable indicator of body fat content for most people. BMI does not measure body fat directly; we calculate it indirectly from a person's height and weight. Even so, research has shown that BMI correlates well to direct measures of body fat, such as weighing a person underwater and using dual energy x-ray absorptiometry. Those diagnostic tests are quite expensive. BMI is a cost-effective, easy-to-perform method of screening for weight categories that may lead to health problems.

To formula for is

    BMI =  weight / height2
where height is expressed in meters and weight is expressed in kilograms.

For example, UNI men's basketball player Marvin Singleton is listed as 6'-6" tall and 237 pounds. After we convert those values to metric, we find that Singleton has a BMI of 27.4.

In order to help people and medical professionals screen for potential health problems, the CDC classifies BMI scores into four weight categories, as listed in this table:

BMI Weight Status
Below 18.5 Underweight
Below 25 (but at least 18.5) Normal
Below 30 (but at least 25) Overweight
30.0 and more Obese

According to this table, Singleton's score is classified as overweight. However, the CDC table was created to help assess the health of average adults, not competitive athletes. Even for average adults, the BMI and CDC categories are only one indicator of health, to be interpreted in the context of many other indicators.

Let's write a program to compute BMI. Your program will consist of four steps:

  1. Prompt the user for the person's height and weight. Our users will primarily American, so ask for the height in feet and inches, and the weight in pounds.

  2. Convert the inputs to equivalent metric values. One inch equals 2.54 centimeters, and one pound equals 0.453592 kg.

  3. Compute BMI.

  4. Display the person's BMI, along with its CDC category.

You can check your program's results using the Adult BMI Calculator provided by the CDC.

Name your program body_mass_index.py.



Spec 2: Compound Interest

Many people do not fully appreciate the effect of compound interest, which is interest that is added to the principal of a deposit or loan so it also earns interest from then on. It is one of the most powerful financial tools at our disposal. It can also cost us greatly when we are the ones paying interest.

For example, suppose I deposit $100 in the bank and earn 5% interest each year. At the end of the first year, I will have earned $5. If I withdraw the interest and spend it, my account balance remains at $100; at the end of the second year, I will earn another $5. But I leave the first year's interest in the account, at the end of the second year, I will earn 5% on $105, or $5.25. That may not seem like a big difference, but after 20 years I will have earned $155.33 in interest, instead of $100. To earn that much with simple interest, you would need to receive more than 7.75% each year.

This is small example. For more frequent compounding, higher interest rates, and longer time periods, and the effect is even more powerful.

Let's write a program to calculate compound interest on a deposit or loan. Your program will:

  1. Prompt the user for three values:

  2. For each year of the investment,

Use a while statement or for statement to repeat the calculation the correct number of times. Use the float() function to convert the input string for the interest rate. When displaying balances, you don't need to worry about rounding the real numbers to two digits yet.

You can check your program's results using the Compound Interest Calculator provided by the US Securities and Exchange Commission at investor.gov. With that calculator, set "monthly addition" to 0 and "compound interest" to 1 time per year. (We will extend our calculator with these and other helpful features later.)

Name your program compound_interest.py.



Demonstration of Correctness

Start a fresh Python shell. Run each of your programs in the shell on three to five examples that demonstrate the correctness of your program. Save your shell to a text file named interactions.py.



Deliverables

By the due date and time, submit:

Use the on-line submission system.

Make sure that your program meets the course programming standards.



Eugene Wallingford ..... wallingf@cs.uni.edu ..... September 13, 2014