Skip to main content.

Goals

After the lab, you should know how to

  1. solve more advanced loop problems
  2. utilize built-in and imported functions in Python
  3. use functions defined within the same module

Objective: Practice Using Linux

Set up for Lab 3

  1. Run runHelpClient & to get our handy tool running.
  2. Create a directory called lab3 in your cs111 directory. Your programs and the output for this lab will all be saved in the lab3 directory.
  3. Copy all the .py files in /csdept/courses/cs111/handouts/lab3/ into your lab3 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.

  1. (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 a for 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 = ?
    ...
    
  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
    

  3. (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.
  4. (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 the math module. Select a "reasonable" number of digits for precision in the result you display to the user.
  5. 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.

  6. Reference Material for Graphics Programming

  7. (15) Copy your program lab2.7.py from last lab and name it lab3.6.py in the lab3 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 the move method on each of them.

    There will be no saved output for this program.

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.

  1. Submit your lab3 directory for grading by running the turnin.sh lab3 command. If you have every thing set up correctly, this will copy your lab3 directory into your turnin directory so that you and I (only) can see your submission. (If you run the turnin.sh script again, it will copy the current directory and create a backup of the previous submission.)
  2. Clean up: jEdit makes backup files and appends "~" to the name of your file. Delete any "~" files from your lab directory.
  3. Before printing, move graphics.py out of your cs111/lab3 directory; otherwise, the print out will be too long. (Check out the mv command.) You can move that file back into your lab3 directory after you've printed. If you have a file graphics.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.)
  4. From your cs111 directory, create your printed lab assignment, using the createPrintableLab 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.

  5. View your file using the evince command.
  6. 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)