CS21 Lab1: First programs

Due 11:59pm Tuesday night, September 9

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.

Then move into your cs21/labs/01 directory and create the python programs for lab 1 in this directory (handin21 looks for your lab 1 assignments in your cs21/labs/01 directory):

% cd cs21/labs/01
% pwd
  /home/your_user_name/cs21/labs/01

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

1. 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!



2. 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 

It is okay if your program prints one square value per line (either way is fine):
$ 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 


3. 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

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.