CS21 Lab 1: First Programs

Due 11:59pm Tuesday 27 January 2009

This lab assignment will require you to write some basic programs in python. Run update21, if you haven't already, to create the cs21/labs/01 directory and copy over any skeleton programs. The program handin21 will only submit files in this directory.

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

Weight conversion

On the planet Jupiter, your weight would be 2.53 times what it is on Earth. Write a program to convert the user's weight to what it would be on Jupiter. Here's a sample run:

$ python weight.py

This program converts your weight on Earth to 
what it would be on the planet Jupiter. Please
enter your weight below.

weight on Earth (in pounds): 160

On Jupiter, you would weigh 404.8 pounds!

Print squares
In the same labs directory, create a program that asks the user for an integer (n) and then prints out all squares from 1 to n.
$  python squares.py 

This program will print out squares (i*i) for i going 
from 1 to n.  Please enter an integer for n below.

n:  7 

1 4 9 16 25 36 49 

Radioactive decay
In radioactive decay, unstable atoms emit radiation and transform into a different type of atom. The half-life is the time it takes for half of the original atoms to transform.

In the same labs directory, create a program that asks the user for the original number of grams in a radioactive sample, and the number of half-lives to show. Have the program show how many grams of this sample are left after each half-life.
$ python halflife.py 

This program shows how the number of grams in a radioactive sample
decreases for each half-life. Please enter the starting number of 
grams and the number of half-lives to show.  

starting number of grams: 1000 
number of half-lives to show: 4 
--------------------------------
half-life  0 .... # grams =  1000
half-life  1 .... # grams =  500.0
half-life  2 .... # grams =  250.0
half-life  3 .... # grams =  125.0
half-life  4 .... # grams =  62.5
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.