Lab 3: Functions, Modules, Using APIs, and Animation

Goals

After the lab, you should know how to

  1. solve loop problems
  2. utilize built-in and imported functions in Python

Objective: Practice Using Linux

Set up for Lab 3

  1. Run labhelp 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 /csci/courses/cs111/handouts/lab3/ into your lab3 directory.

Objective: Textbook and Pre-Lab Checkpoint

On your laptop or on the lab machine, log into the interactive textbook. Check that the grades for the pre labs (up through pre lab 2) are what you expect. If they aren't, please talk to me.

Select Practice under the User Profile to practice some of the material you've learned so far. You don't need to practice right now -- just know that that feature exists.

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. (10 pts) Using three variables (operand1, operand2, and result), calculate and display result = operand1 % operand2. Use assignment and print statements and a for loop to show the results of operand1 % operand2, where operand1 = 12 and operand2 increases from 1 to 14. Example output (without the appropriate values filled in):
    12 % 1 = ?
    12 % 2 = ?
    ...
    
  2. (17 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. (12 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. After running the program, change your constant to 200 and run the program again to demonstrate how easy it is to change the constant and the code to change accordingly.
  4. (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 pi defined in the math module. Select a "reasonable" number of digits for precision in the result you display to the user.
  5. (13 pts) The Virginia Pick 4 lottery game uses four magic ping pong ball machines to generate the winning lottery number. Each machine has ping pong balls with numbers between 0 and 9, inclusive. Your job is to simulate generating the magic number. Example output is below. Note that numbers can be repeated and how the number is displayed. As always, break the problem into pieces and solve. (Note: you should not be using strings or anything we haven't been using frequently in class.) Your output should match the format shown below exactly. Your code should be such that if we changed the game to Pick 20, it would be a relatively simple change.
    The Pick 4 number is 8894.
    
    The Pick 4 number is 0714.
    
  6. 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 applies the accumulator design pattern.

  7. Reference Material for Graphics Programming

  8. (16) Copy your program lab2_7.py from last lab and name it lab3_7.py in the lab3 directory. Add some sort of animation to the picture you created last week. You can add more objects or otherwise update your picture from 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. See the textbook for examples of using for loops without range.

    Besides movement, you can also change colors and/or undraw/draw objects for your animation.

    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. Make sure you're in your lab3 directory.

    Clean up

    Move graphics.py into your cs111 directory. (see mv. Note where the cs111 directory is with respect to where you're running this command from.)
    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.)

  2. Create your printable lab assignment, using the createPrintableLab command:
    createPrintableLab <labdirname>

    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.

  3. View your file using the evince command. It should only be a few pages. If it's longer than that, you are probably printing graphics.py. Request help.
  4. Print your lab from evince, as you have in previous labs.
  5. Move your graphics.py back into your lab3 directory.
  6. Submit your lab3 directory for grading by running the turnin 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 need to run the turnin.sh script again, it will copy the current directory and create a backup of the previous submission.)

Labs are due at the beginning of Friday's class. 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)