Lab 1: Numeric and String Operations
Goals
After the lab, you should know how to
- move/rename files in Linux
- solve simple arithmetic problems in Python
- solve simple string problems in Python
Objective: Practice Using Linux
- 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
trianglearea.py
from your
lab0
directory into your lab1
directory.
- To rename files or move them from one directory to another, you
can use the
mv
command. Move the
trianglearea.py
file into the file lab1.8.py
using the command in the lab1
directory:
mv trianglearea.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.
After you've developed a correct solution to each program, restart
IDLE or close and reopen the IDLE "shell" 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) 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;
- (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
- (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. The final
temperature displayed should only go out two decimal places.
Output should look similar to:
% python lab1.4.py
This program prints the Celsius temperature
given a Fahrenheit temperature.
Enter a Fahrenheit temperature: 70
70 degrees F is 21.11 degrees C
- (15 pts) Write a program that demonstrates the importance of
operator precedence. Your program will take in three integers
from a user and place them in the variables a, b, and c. Then, print
the result of a Python expression, using those three numbers, that has
no parentheses. Then, show a different result from the same expression
with one added pair of parentheses.
Example output (with fake numbers):
% python lab1.5.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. Your output should be formatted to four decimal places.
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)
Create a questionaire-development program that creates questionaire
questions, based on what the user wants to compare. The generated
question always has the format "Which do you like better: X or Y?"
An example run is shown below:
Welcome to the simple questionaire program!
What do you want to compare? Apples
What do you want to compare Apples to? Oranges
Which do you like better: Apples or Oranges?
- (10 pts) Modify your trianglearea.py program from last week to use
the constant
pi
from the math
module.
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.)
- Turn in your printed lab assignment, using the
printLab.sh
command.
We've made the print process a little easier. Instead of typing out
the whole long command name as you did in the last lab to create the
printable file, now, you just have to type:
printLab.sh
<labdirname>
Again, you should probably print from the labs
directory.
Print the file using the lpr
command introduced in the
first lab.
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 2:25
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