High-Level Programming with Python


In class challenges

  1. The Body Mass Index (BMI) is a screening measure meant to approximate a person's body fat based upon his or her height and weight. Let W represnet a weight (measured in pounds) and H represent a height (measured in inches). Then the index is calculated according to the formula BMI = 703 W / H2

    This statistic can be used to classify a person into one of four categories:

    Wirte a program that asks the user for the height and weight and reports the BMI and the appropriate classification. A sample session might appear as follows:

          What is your height (in inches):  70
          What is your weight (in pounds):  140
          Your BMI is 20.0857
          This is in the normal range.
          

  2. Print the numbers from 10 down to 1.

  3. Print all odd numbers from 1 to 20.

  4. Input an integer N, and calculate the sum 1 + 2 + 3 + ... + N

  5. Given a positive integer k, calculate the factorial of k defined as k! = k * (k-1) * (k-2) * ... * 2 * 1

  6. The precise value of the mathematical constant pi is equal to the following infinite serioes: pi = 4 * (1/1 - 1/3 + 1/5 - 1/7 + ...).

    Write a program that asks the user for the number of desired terms and then prints an approximation to pi by computing the first so many terms of this series.

  7. Write a program that prints an n-level staircase made of text, such as the example for n=4 shown here.

                *
              * *
            * * *
          * * * *
          

  8. Write a program that prints out an n x n multiplication table for a chosen value of n. As a model, here is a simple 4 x 4 version.

          1   2   3   4
          2   4   6   8
          3   6   9  12
          4   8  12  16
          

    Note: to align columns, you can force a value to be printed right-justified with a given using a syntax str(val).rjust(3) when printing the value of variable val.

  9. Redo the previous problem, yet with a nicer output format:

             |   1   2   3   4
          ---+----------------
           1 |   1   2   3   4
           2 |   2   4   6   8
           3 |   3   6   9  12
           4 |   4   8  12  16
          


Last modified: Tuesday, 16 October 2007