CS21 Lab 2: loops and strings

Due 11:59pm Tuesday, 01 February

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 asterisks 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(2-80): 15
height(2-20): 5
***************
*             *
*             *
*             *
***************

$ python box.py

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

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

You may assume that the user enters a width between 2 and 80 (inclusive) and a height between 2 and 20 (inclusive); your program does not need to work correctly if the user enters a non-number or value outside those ranges.



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

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

$ python funstr.py

Enter text: abcdefghijklmnop

abcdefghijklmnop
abcdefghijklmno
abcdefghijklmn
abcdefghijklm
abcdefghijkl
abcdefghijk
abcdefghij
abcdefghi
abcdefgh
abcdefg
abcdef
abcde
abcd
abc
ab
a



4. Bob Barker's nightmare

You discover a pair of cats inside Willets Hall. Assume that a cat colony breeds as follows:

Based on these numbers, write a program (in the file called kittens.py) to show how the Willets cat population will change over the next 20 years. Your program should output the cat population every year for 20 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.

To help you check your program, the population in 2012 is 6 cats (2 cats from 2011 plus 4 kittens, with no adoptions), the population in 2013 is 18 cats (6 from 2012 plus 12 kittens, with no adoptions), the population 2014 is 53 (18 cats from the previous year, 36 kittens from the 9 cat pairs, and 1 adoption), and the population in 2015 is 152 (53 cats from the previous year, 104 kittens from the 26 full cat pairs, and 5 adoptions).

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.