CS21 Lab 1: First Programs

Due 11:59pm Tuesday 9 September 2008

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 (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 shows how many of the original atoms are left after a given number of half-lives. Also ask the user for the original number of atoms.
$ python halflife.py 

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

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