Lab 1: Numeric Operations
Goals
After the lab, you should know how to
- move/rename files in Linux
- solve simple arithmetic problems in Python
Objective: Practice Using Linux
Adding the pwd to your prompt
To make it a little easier for you to know what directory you are
in when you're using a Linux terminal (i.e., using the command-line interface),
you're going to update a customization file.
- In your home directory, open the file
.bashrc
with jEdit.
Note that since the file name begins with a ".", it is hidden
from jEdit. You will have to explicitly type the name of the file
in jEdit's browse window or use jedit .bashrc
to open
the file.
- At the bottom of the file, add the following line:
PS1='[\u@\h \W]> '
- Save the file.
- In jEdit, open .bash_profile
- Add the following line at the end of the file:
export PS1
- Save the file.
- You now need to update the shell with the changes to your
profile. Use the command:
source .bash_profile
You should see a change to your prompt. It should look something like:
[username@machine ~]$
The ~ represents your home directory. When you change
directories, your prompt will reflect those changes.
From now on, whenever you open a new terminal, your
prompt should look like this.
Setting Up for Lab 1
- Create a directory called
lab1
in your
labs
directory. Your programs and the output for this lab
will all be saved in the lab1
directory.
- Copy the file
area.py
from
the /home/courses/cs111/handouts/lab1/
into
your lab1
directory.
- To rename files or move them from one directory to another, you
can use the
mv
command. Move the
area.py
file into the file lab1.8.py
using the command in the lab1
directory:
mv area.py lab1.8.py
- List the contents of your directory. You should have only one file
(called
lab1.8.py
) in the directory.
Objective: Programming in Python
We'll practice writing several Python programs, each in their own text
file. Name the files lab1.1.py through lab1.8.py.
Your programs will be graded on correctness, style, and how well
you tested them. Make sure you adhere to the good development and
testing practices we discussed in class, use good variable names
After you've developed a correct solution to each program, close the
IDLE "shell" and reopen it by running the program again (using F5),
demonstrate that the program works using several good test
cases, and save the output to a file named lab1.x.out, where x is the
problem number.
- (15 pts) This problem has several parts. You will create a program
in the first step and modify it for each subsequent step. Note how the
progression of steps for this problem adheres to the good development
practices we dicussed in class.
- Create three variables (i, j, and result) to calculate and
display result = i² + 3j - 5 for the case where i=7 and
j=2. Your code will not look exactly like this formula; think, and
test your ideas.
- Change the name of the variable i to i21. Be sure to change the
name everywhere the variable is used. Execute it to show it
works.
- Now edit the file again and change the name to 21i. What error
message do you get? Record in comments the error message. Revert your
program back to the correct variable name.
- Modify your program so that the program prompts the user for
values of i and j.
- (10 pts) How do you measure, measure a year? The
musical Rent asks how you would measure a year and suggests
love as the metric. Since we don't have a type to represent love,
we'll measure a year in minutes. Write a program that computes the
number of minutes in a year and plugs it into part of the chorus
of Seasons Of Love.
Note: do not recompute the number of minutes each time it is
used in the chorus. Compute the minutes once and save it in a
variable that you use in the print statements.
Output should look similar to
% python lab1.2.py
525600 minutes, 525600 moments so dear.
525600 minutes - how do you measure, measure a year?
- (10 pts) Create a program that computes your birth year, given
your age and the current year. Output should look similar to:
% python lab1.3.py
This program determines your birth year
given your age and current year
Enter your age: 20
Enter the current year: 2006
You were either born in 1986 or 1985
- (10 pts) You will create five variables: i, j, x, y, and result.
Set i to
9, j to 2, x to 9.0 and y to 2.0. Now write a series of six pairs of
assignment and print statements as follows:
- Set result to i/j; print result;
- Set result to j/i; print result;
- Set result to x/y; print result;
- Set result to y/x; print result;
- Set result to i/y; print result;
- Set result to float(i)/j; print result;
(We'll talk more about what's going on here on Wednesday.)
- (15 pts) Create a program that converts a given Fahrenheit
temperature to Celsius. The formula to convert is C=5/9(F-32). Again,
note that you cannot use this formula exactly in your program.
Output should look similar to:
% python lab1.5.py
This program prints the Celsius temperature
given a Fahrenheit temperature.
Enter a Fahrenheit temperature: 70
70 degrees F is 21.111111111111111 degrees C
Note: The output is not very pretty yet. We'll learn how to
format the output more nicely soon.
Note: When you demonstrate this program, especially think
about what are good test cases for this program. What answers do you
know?
- (15 pts) Write a program that demonstrates the importance of
operator precedence. Your program will get three integers
from a user and assign them to the variables a, b, and c. Then, print
the result of a Python expression that has no parentheses, using those
three numbers. Then, show a different result from the same expression
with one added pair of parentheses.
Example output (with fake numbers--I'm not showing an equation):
% python lab1.6.py
Enter a: 12
Enter b: 34
Enter c: 5
The result of <print out your equation here> is 51.
The result of <print out your equation with parentheses here> is 15.
- (15 pts) [From Zelle, Chapter 3, Problem 3] Write a program that
determines the total molecular weight of a molecule based on the
number of hydrogen, carbon, and oxygen atoms it contains. You should
use the following weights.
Atom | Weight (g/mol) |
H | 1.0079 |
C | 12.011 |
O | 15.9994 |
You program should prompt the user for the number of atoms of
each type and display the total weight with the appropriate
units.
A sample run is shown below:
Enter number of Hydrogen atoms: 3
Enter number of Carbon atoms: 2
Enter number of Oxygen atoms: 1
Weight of molecule is 43.0451 g/mol
- (10 pts) Modify the
area.py
program to compute the area of a
triangle. (What information do you need from the user?)
Finishing up: What to turn in for this lab
- Copy your lab1 directory into the
turnin
directory.
(Review the UNIX handout if you don't
remember how to do that.)
- Cleanup: jEdit and IDLE may make backup files. The backup
files have an "~" appended to the name of your file. Delete any "~"
files from your lab directory. (What is the UNIX command that you can
use to delete all of them at once?)
- You will print out your lab and turn that in on Friday. Don't
forget to write out the Honor Pledge and sign your lab. Also,
staple all pages together and put your printed name at the top of the
page.
Create the printable file of your lab using the
printLab.sh
command.
Recall that the command you use is
printLab.sh <labdirpath>
and that you should probably print from the labs
directory. This command should create the file lab1.ps
To view the file you created, use the command gv lab1.ps
&
Print the file using the lpr
command introduced in the
first lab.
The command has the form
lpr -P<printername> <filetoprint.ps>
Labs are due at the beginning of Friday's class. You should hand
in the printed copy at the beginning of class, and the electronic
version should be in the turnin
directory before 1:20
p.m. on Friday.
Ask well before the deadline if you need help turning in your
assignment!
Grading (100 pts)
- Python programs: 100 pts; see above for breakdown