CS21 Lab 1: First programs

Due by 11:59pm Tuesday, 26 January 2010

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

Note: Problem 1 can be done after Wednesday's lecture, Problems 2 and 3 require material that will be covered in Friday s lecture.

1. Miles to Kilometers Conversion

One mile is is equivalent to 1.609 kilometers. Write a program to convert a number miles entered by the user to its equivalent number of kilometers. Here are some sample runs:

$ python miles_to_km.py

 This program converts a number of miles
 to its equivalent number of kilometers.

 Enter the number of miles: 4
 The number of kilometers in 4 miles is 6.436

$ python miles_to_km.py

 This program converts a number of miles
 to its equivalent number of kilometers.

 Enter the number of miles: 6.2
 The number of kilometers in 6.2 miles is 9.9758



2. Print Cubes
In the same labs directory, create a program that asks the user for an integer (n) and then prints all 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 (either way is fine):
$ 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
In the same labs directory, write a program that prints a conversion table of US Dollars to Chinese Yuans. Use a conversion rate of 1 US Dollar is 6.8258 Chinese Yuan. 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 dollar and euros amount 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 dollars_to_yuan.py

This program prints a conversion table of US dollars
to Chinese Yuan of a range given by the usr.

Enter the starting dollar amount for the range: 4
Enter the ending dollar amount: 12

Dollars         Chinese Yuan
-----------------------------
  4 		27.3032
  5 		34.129
  6 		40.9548
  7 		47.7806
  8 		54.6064
  9 		61.4322
  10 		68.258
  11 		75.0838
  12 		81.9096


$ python dollars_to_yuan.py

This program prints a conversion table of US dollars
to Chinese Yuan of a range given by the usr.

Enter the starting dollar amount for the range: 31
Enter the ending dollar amount: 39

Dollars 	Chinese Yuan
-----------------------------
  31 		211.5998
  32 		218.4256
  33 		225.2514
  34 		232.0772
  35 		238.903
  36 		245.7288
  37 		252.5546
  38 		259.3804
  39 		266.2062
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.

Please wait to run handin21 until Friday afternoon. It will not be set up to accept lab 1 assignments until then.