CS21 Lab 1: First programs

Due by 11:59pm Tuesday, September 6, 2011

This lab assignment will require you to write three programs in python. First, run update21. This will create the cs21/labs/01 directory and copy over three starting point files for your programs. The program handin21 will only submit files in this directory. Next, move into your cs21/labs/01 directory and begin working on the python programs for this lab. The pwd command helps you verify that you are in the correct sub-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.

After Wedensday's class you should be able to solve problem 1. On Friday we will cover for loops, which are necessary for solving problems 2 and 3.

1. Weight on the Moon

Due to it's smaller mass, your weight on the Moon is about 1/6th your weight on the Earth. Use vim to edit the file moonweight.py in the cs21/labs/01 directory and write a program to convert a weight given in pounds to what that weight would be on the Moon. Here are some sample runs of such a program:

$ python moonweight.py

This program calculates what you would weigh on the moon.
Please enter your weight on the Earth.

Earth weight: 600
On the Moon, you would weigh  100.0

$ python moonweight.py

This program calculates what you would weigh on the moon.
Please enter your weight on the Earth.

Earth weight: 165
On the Moon, you would weigh  27.5

Extra Challenge

Note: extra challenges are just for fun (i.e., no bonus points). Please only attempt them after completing your regular assignment.

See Your Weight On Other Worlds for what you would weigh on the other planets in the solar system. Add a few other planets to your above program.

2. Times Table

Johnny needs help learning his multiplication tables. Let's write a program to display multiplication results.

Edit the file timestable.py in the cs21/labs/01 directory and write a program that asks the user for an integer n and then prints all of the products of that number (n) and the numbers 1 to 12.

Here is an example run of a working program:

 
$ python timestable.py

This program calculates the product of n x the numbers from 1 to 12.
Please enter a number for n.

n: 5
5  x  1  =  5
5  x  2  =  10
5  x  3  =  15
5  x  4  =  20
5  x  5  =  25
5  x  6  =  30
5  x  7  =  35
5  x  8  =  40
5  x  9  =  45
5  x  10  =  50
5  x  11  =  55
5  x  12  =  60

Note: you should be using a loop for this!

3. Exchange Rate Conversion Table

Last week 1 US Dollar was worth about 6.4 Chinese Yuans. Edit the file dollars2Yuans.py in the directory cs21/labs/01 and write a program that prints a conversion table of US Dollars to Yuans. The program user will enter a starting and ending dollar value for the range of conversions to print.

To get the values to line up in columns you can print out one or more tab characters ("\t") between printing the two amounts on each line. For example, try these print statements in python and see what happens:

print 3, "\t\t", 4
print 3, "\t", 4
Don't worry about getting the decimal points to line up for values with different numbers of digits before the decimal point (e.g. 99.123 and 999.123). We will learn how to do that later this semester.

Here are some example runs of a working program:

$ python dollars2Yuans.py

This program prints a conversion table for US Dollars to Chinese Yuans.

Enter the starting dollar amount: 10
  Enter the ending dollar amount: 20

US Dollars      Chinese Yuans
10              64.0
11              70.4
12              76.8
13              83.2
14              89.6
15              96.0
16              102.4
17              108.8
18              115.2
19              121.6
20              128.0

$ python dollars2Yuans.py

This program prints a conversion table for US Dollars to Chinese Yuans.

Enter the starting dollar amount: 35
  Enter the ending dollar amount: 45

US Dollars      Chinese Yuans
35              224.0
36              230.4
37              236.8
38              243.2
39              249.6
40              256.0
41              262.4
42              268.8
43              275.2
44              281.6
45              288.0

Submit

Once you are satisfied with your programs, hand them in by typing handin21 at the unix prompt.

Note: handin21 will not be ready for lab 1 until this weekend

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.