echo $title ?>
Goals
After the lab, you should know how to
- solving more advanced loop problems
- utilize built-in and imported functions in Python
- use functions defined within the same file
Objective: Practice Using Linux
Set up for Lab 3
- Run runHelpClient & to get our handy tool running.
- Create a directory called
lab3in yourcs111directory. Your programs and the output for this lab will all be saved in thelab3directory. - Copy all the
.pyfiles in/csdept/courses/cs111/handouts/lab3/into yourlab3directory.
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.
- (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.
- (12 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
pidefined in themathmodule. 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 15 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) Add some sort of animation to the picture you created last
week. Copy your program
lab2.7.pyand name itlab3.5.py. 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
forloop and iterate over each of the objects, calling themovemethod on each of them.There will be no saved output for this program.
- (20) Open lab3.6.py, which you copied at the beginning of
lab. If
lab3.6.pylooks like graphics.py, recopy the file. (See the set up instructions above.) Someone previously overwrote the file, but it's been fixed. Read through the functions in the file and their documentation. Then, modify the program so that themainfunction implements what is described in its comments, e.g., draws and moves bugs, using functions defined within the program.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.
- Submit your
lab3directory for grading by running the turnin.sh lab3 command. If you have every thing set up correctly, this will copy yourlab3directory into yourturnindirectory so that you and I (only) can see your submission. (If you run theturnin.shscript again, it will 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.pyout of yourcs111/lab4directory; otherwise, the print out will be too long. (Check out themvcommand.) You can move that file back into yourlab3directory 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
cs111directory, create your printed lab assignment, using thecreatePrintableLabcommand:
createPrintableLab <labdirname>Again, you should probably print from the
cs111directory.If you see a message about an "unescaped sequence", that probably means you still have an image in the
lab3directory. Remove the image from the directory and repeat this step. - View your file using the
evincecommand. - Print the file using
the
lprcommand.
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 (100 pts)
- Python programs: 100 pts; see above for breakdown