CS21 Lab 2: loops and strings

Due 11:59pm Tuesday, 14 September

Run update21, if you haven't already, to create the cs21/labs/02. Then cd into your cs21/labs/02 directory and create 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

Your programs are graded on both correctness and style. Please review the comments regarding programming style on the main page.

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. 
Please enter the width and height of the box.

 width: 15
height: 5
...............
.             .
.             .
.             .
...............

$ python box.py

This program draws a box. 
Please enter the width and height of the box.

 width: 7
height: 9
.......
.     .
.     .
.     .
.     .
.     .
.     .
.     .
.......

If the user enters a very large width or height, it's OK if your box is not pretty (or does not look much like a box).



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

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

A word of warning: it might be tempting to solve the funstr problem using the Accumulator Pattern we've discussed in class, but we do not recommend that approach here.

4. Population estimation

Assuming that the current US population is 310,000,000, how much will it change in 5 or 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 output data.

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 check your program, the population after two years should be around 315,960,648. There may be slight variations in this number, depending on how you handle ints and floats, but it should definitely be near 315,960,000.

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.