CS21 Lab 2: Loops, numbers, and strings

Due 11:59pm Tuesday, 14 September

Run update21 to create the cs21/labs/02 directory and starting point files. Then cd into your cs21/labs/02 directory and edit the python programs for lab 2 in this directory (handin21 looks for your lab 2 assignments in your cs21/labs/02 directory):


$ update21
$ cd
$ cd cs21/labs/02
$ pwd
  /home/your_user_name/cs21/labs/02


1. Box

Write a program, in the file named box.py, that asks the user to enter two values: the width and height of a box. Your program should then draw a box of dots (periods) in the terminal window with the given width and height.

Here are two examples of the box.py program:


$ python box.py

This program draws a box.

Enter width (2-80): 15
Enter height (2-20): 5

...............
.             .
.             .
.             .
...............

$ python box.py

This program draws a box.

Enter width (2-80): 7
Enter height (2-20): 9

.......
.     .
.     .
.     .
.     .
.     .
.     .
.     .
.......
Note that you will need to draw the box one row at a time. Once you have printed a given row you cannot go back to a previous row.

2. Body Mass Index (BMI)

BMI is a number calculated from a person's weight and height. The formula for BMI is weight/(height squared), where weight is in kilograms and height is in meters.

Write a program, in the file called bmi.py, that prompts for weight in pounds and height in inches, converts the values to metric, and then calculates the BMI.

Here is an example of the bmi.py program:


$ python bmi.py

This program calculates Body Mass Index also known as BMI.

enter weight in pounds: 160
enter height in inches: 69

BMI = 23.628

Find a BMI chart on the web and check that your program is running correctly.

Note: 1 pound = 0.45359237 kg, 1 inch = 0.0254 m

3. Fun with strings...
Write a program, in the file called funstr.py, to read in a string of text and output the text as follows:
$ python funstr.py

Enter text: we love CS

w
we
we 
we l
we lo
we lov
we love
we love 
we love C
we love CS
we love CS
we love C
we love 
we love
we lov
we lo
we l
we 
we
w

Here is another sample run:

$ python funstr.py

Enter text: TestMe

T
Te
Tes
Test
TestM
TestMe
TestMe
TestM
Test
Tes
Te
T


4. Population estimation

Assuming that the current US population is 310 million, how much will it change in 10 years?

The US Census provides approximate rates of change:

Based on these numbers, write a program (in the file called pop.py) to show how the population will change over the next 10 years. Your program should output the approximate population every year for 10 years. You can format the output any way you like, but it should include some introductory text that explains what the program is doing, and it should clearly present the results.

Hint: If there's a birth every 7 seconds, how many births are there in a year (it's fine to assume a year is exactly 365 days)?

Check: To help you verify that your program is working correctly, the population after two years should be approximately 315,960,650 (it may differ slightly depending on how you do your floating point calculations).

Submit
Once you are satisfied with your programs, hand them in by typing handin21 at the unix prompt. You may run handin21 as many times as you like, and only the most recent submission will be recorded. This is useful if you realize after handing in some programs that you'd like to make a few more changes to them.