echo $title ?>
Goals
After the lab, you should know how to
- solve more advanced loop problems
- utilize built-in and imported functions in Python
- use functions defined within the same module
Objective: Practice Using Linux
Set up for Lab 3
- Run runHelpClient & to get our handy tool running.
- Create a directory called
lab3
in yourcs111
directory. Your programs and the output for this lab will all be saved in thelab3
directory. - Copy all the
.py
files in/csdept/courses/cs111/handouts/lab3/
into yourlab3
directory.
Objective: Programming in Python
We'll practice writing several Python programs, each in their own text file. Name the files lab3.1.py through lab3.6.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. Your code should be readable and your output should be useful and well-formatted.
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 lab3.x.out, where x is the problem number.
- (14 pts) Using three variables (operand1, operand2, and result),
assign them values to calculate and display
result = operand1 % operand2
. Use assignment and print statements and afor
loop to show the results of operand1 % operand2, where operand1 = 6 and operand2 increases from 1 to 9. Example output (without the appropriate values filled in):6 % 1 = ? 6 % 2 = ? ...
- (18 pts) How long would a road trip take at various travel
speeds, e.g., light traffic, or rush hour? Write a program to find
out! Your program should print a table of travel times for a 50 mile
trip at each speed between the minimum (40) and maximum speeds (50),
including both endpoints, at one mile per hour increments. Round
the travel time to at most 2 decimal places.
After you get that working, modify your program to prompt the user for the minimum and maximum travel speed in miles per hour as well as the trip distance, all as ints.
Below are some sample runs showing how your program should work.
Enter slowest speed, in mph: 40 Enter fastest speed, in mph: 50 Enter driving distance in miles: 50 Times for a 50 mile trip: 40 mph -> 1.25 hours 41 mph -> 1.22 hours 42 mph -> 1.19 hours 43 mph -> 1.16 hours 44 mph -> 1.14 hours 45 mph -> 1.11 hours 46 mph -> 1.09 hours 47 mph -> 1.06 hours 48 mph -> 1.04 hours 49 mph -> 1.02 hours 50 mph -> 1.0 hours
Enter slowest speed, in mph: 25 Enter fastest speed, in mph: 30 Enter driving distance in miles: 45 Times for a 45 mile trip: 25 mph -> 1.8 hours 26 mph -> 1.73 hours 27 mph -> 1.67 hours 28 mph -> 1.61 hours 29 mph -> 1.55 hours 30 mph -> 1.5 hours
- (15 pts) Write a program that sums all the odd integers between 0 and 100 and prints the total. Define a constant to stop your loop, i.e., define a constant to say the upperbound of the odd integers you're adding up.
- (13 pts) Write a program that calculates the area of a circle.
Get the radius of the circle as input from the user. Use the most
precise value of π available to you, i.e., use the
constant
pi
defined in themath
module. Select a "reasonable" number of digits for precision in the result you display to the user. - Challenge Problem. (20) The Fibonacci sequence
is 0, 1, 1, 2, 3, 5, 8, 13, ... The pattern is that the nth number is
Fn=Fn-1 + Fn-2 for n greater than
1. The initial values of the sequence are defined as F0=0 and F1=1.
Write a program that computes the first 20 numbers in the Fibonacci
sequence.
If you're having difficulty solving this problem, think about: How many times does this loop need to execute? What needs to be repeated? Try solving this problem by hand, calculating and writing out the results. You won't receive any help until we see that you have something written out. Hint: this is a modification of the accumulator design pattern.
- (15) Copy your program
lab2.7.py
from last lab and name itlab3.6.py
in thelab3
directory. Add some sort of animation to the picture you created last week. For example, you could make an entire face move around the window or you could have a feature of a face move (such as a blinking eye). Note: this is not simply the click and move we did before we did animation. You can allow a user to click to move things, but you must animate that movement.If you want to animate a group of objects, moving together, you can use a
for
loop and iterate over each of the objects, calling themove
method on each of them.There will be no saved output for this program.
Reference Material for Graphics Programming
Finishing up: What to turn in for this lab
Review the Unix commands for submitting your lab.
Note that each command below links to a page with more information about using the command.
- Submit your
lab3
directory for grading by running the turnin.sh lab3 command. If you have every thing set up correctly, this will copy yourlab3
directory into yourturnin
directory so that you and I (only) can see your submission. (If you run theturnin.sh
script again, it will copy the current directory and create a backup of the previous submission.) - Clean up: jEdit makes backup files and appends "~" to the name of your file. Delete any "~" files from your lab directory.
- Before printing, move
graphics.py
out of yourcs111/lab3
directory; otherwise, the print out will be too long. (Check out themv
command.) You can move that file back into yourlab3
directory after you've printed. If you have a filegraphics.pyc
(by accidentally running using Python2 instead of Python3), you should delete that file before printing. In other words, you should only have the .py files you wrote, the .out files you created, and a__pycache__
directory in your directory when you print. (The latter is not printed.) - From your
cs111
directory, create your printed lab assignment, using thecreatePrintableLab
command:
createPrintableLab <labdirname>Again, you should probably print from the
cs111
directory.If you see a message about an "unescaped sequence", that probably means you still have an image in the
lab3
directory. Remove the image from the directory and repeat this step. - View your file using the
evince
command. - Print the file using
the
lpr
command.
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 class on Friday.
Ask well before the deadline if you need help turning in your assignment!
Grading (95 pts)
- Python programs: 95 pts; see above for breakdown