Lab 2: Arithmetic, Basic Loops, Functions, Modules, and Using APIs

Goals

After the lab, you should know how to

  1. more advanced arithmetic problems
  2. solve basic looping problems in Python
  3. utilize built-in and imported functions in Python
  4. using an API to solve problems

Objective: Practice Using Linux

Set up for Lab 2

  1. Create a directory called lab2 in your labs directory. Your programs and the output for this lab will all be saved in the lab2 directory.
  2. Copy graphics.py in /home/courses/cs111/handouts/lab2/ into your lab2 directory.

Objective: Programming in Python

We'll practice writing several Python programs, each in their own text file. Name the files lab2.1.py through lab2.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. 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 lab2.x.out, where x is the problem number.

  1. (12 pts) Distributing Greatest Hits Albums. A supergroup is putting out their Greatest Hits album and needs to know how many cds their album requires. Bands like the Ramones can fit a lot more tracks on a cd than a band like Led Zeppelin.

    Write a program that takes the number of greatest hits and the size of the cds (in terms of the number of tracks) and determines how many cds are needed and how many tracks will have to wait for the next Greatest Hits album.

    This program determines the number of CDs in a Greatest Hits album.
    
    How many greatest hits/tracks do you have? 24
    How many tracks fit on a cd? 10
    
    Your album requires 2 cds
    4 tracks will have to wait for the next Greatest Hits album.
    

    Note: you may have some grammar issues in your output. We don't know how to fix those yet.

  2. (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.

    AtomWeight
    (g/mol)
    H1.0079
    C12.011
    O15.9994

    You program should prompt the user for the number of atoms of each type and display the total weight with the appropriate units.

    Note that your program should use constants for the molecular weights of carbon, hydrogen, and oxygen. (Recall what the conventions are for naming constants. Why does it make sense to make these values constants?)

    Finally, modify your program so that it rounds the molecular weight to 3 decimal places (using a built-in function) and displays that number.

    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.045 g/mol
    
  3. (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.
  4. (12 pts) Using three variables (i, j, and result), assign them values to calculate and display result = i % j. Use assignment and print statements and a for loop to show the results of i % j, where i = 6 and j increases from 1 to 8. Example output (without the appropriate values filled in):
    6 % 1 = ?
    6 % 2 = ?
    ...
    
  5. (13 pts) Write a program that sums all the odd integers between 0 and 100 and prints the total using a loop. Define a constant to stop your loop.
  6. Challenge Problem. (16) The Fibonacci sequence is 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 sequence is defined as F0=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.

  7. Reference Material for Graphics Programming

  8. (10) Using the graphics module, draw a yellow circle with radius 30 and a red square of width 50 in a window that is 400x200 with the name "Practice". Position the circle in the upper-left quadrant of the canvas and the square in the lower-right quadrant. There will be no IDLE output for this program.
  9. (10) Using the graphics module, draw the beginning of a snow-person. Create a canvas with the title "Snow Person". Draw a white circle of radius 50. Clone the circle and move the cloned circle above the first circle (so that the circle appears to be sitting on top of the original circle). Repeat with a third cloned circle. Draw two black, filled-in circles for eyes in the top circle. Note that you should draw one eye, then clone the eye and move it to the appropriate place. There will be no IDLE output for this program.

Finishing up: What to turn in for this lab

  1. Copy your lab2 directory into the turnin directory. (Review the UNIX handout if you don't remember how to do that.)
  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 the graphics.py and graphics.pyc (actually, the latter can be deleted rather than moved) files out of your labs/lab2 directory; otherwise, the print out will be long and screwed up. You can move those files back into your lab2 directory after you've printed. In other words, you should only have the .py files you wrote and the .out files you created in your directory when you print.
  4. Turn in your printed lab assignment, using the printLab.sh command:
    printLab.sh <labdirname>

    Again, you should probably print from the labs directory.

    View your file using the gv command.

    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 1:20 p.m. on Friday.

Ask well before the deadline if you need help turning in your assignment!

Grading (100 pts)