CS21 Lab 1: First programs

Due by 11:59pm Tuesday, January 25 2011

This lab assignment will require you to write three programs in python. 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.

Then move into your cs21/labs/01 directory and begin working on the python programs for this lab. The pwd command lets 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.

1. Pounds to Kilograms Conversion

One pound is equivalent to 0.45359 kilograms. Edit the file poundsToKilograms.py in the cs21/labs/01 directory to write a program to convert a weight given in pounds to its equivalent weight in kilograms. Here are some sample runs:

$ python poundsToKilograms.py
This program converts pounds to kilograms.

Enter a weight in pounds: 125
125 pounds is equivalent to 56.69875 kilograms

$ python poundsToKilograms.py
This program converts pounds to kilograms.

Enter a weight in pounds: 200
200 pounds is equivalent to 90.718 kilograms


2. Print Cubes

Edit the file cubes.py in the cs21/labs/01 directory to write a program that asks the user for an integer n and then prints all of the cubes between 1 and n inclusive.

Here are some example runs of a working program:

 
$ python cubes.py

This program prints the cubes of 1 to n
Enter a value for n: 10
1 8 27 64 125 216 343 512 729 1000

$ python cubes.py 

This program prints the cubes of 1 to n
Enter a value for n: 4
1 8 27 64
It is okay if your program prints one cube value per line rather than all values on the same line as shown below:
 
$ python cubes.py

This program prints the cubes of 1 to n
Enter a value for n: 3
1
8
27


3. Exchange Rate Conversion Table

On September 1, 2010, 1 Euro was worth 1.2796 US Dollars. Edit the file eurosToDollars.py in the directory cs21/labs/01 to write a program that prints a conversion table of Euros to US Dollars. The 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 eurosToDollars.py
This program prints a conversion table for Euros to US Dollars.

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

Euros 	US Dollars
10 	12.796
11 	14.0756
12 	15.3552
13 	16.6348
14 	17.9144
15 	19.194
16 	20.4736
17 	21.7532
18 	23.0328
19 	24.3124
20 	25.592

$ python eurosToDollars.py
This program prints a conversion table for Euros to US Dollars.

Enter the starting Euro amount: 70
Enter the ending Euro amount: 85

Euros 	US Dollars
70 	89.572
71 	90.8516
72 	92.1312
73 	93.4108
74 	94.6904
75 	95.97
76 	97.2496
77 	98.5292
78 	99.8088
79 	101.0884
80 	102.368
81 	103.6476
82 	104.9272
83 	106.2068
84 	107.4864
85 	108.766
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.