Skip to main content.

Goals

After the lab, you should be proficient at

  1. using functions within your program
  2. refactoring your code to use functions

Objective: Review

Review the slides for today.

Objective: Set Up

Run runHelpClient &

As usual, create a directory for the programs and output you develop in this lab.

Copy all the .py files in /csdept/courses/cs111/handouts/lab4/ into your lab4 directory.

Objective: Programming in Python

We'll practice writing several Python programs, each in their own text file. Name the files lab4.1.py, lab4.2.py, etc.

Your programs will be graded on correctness, efficiency, 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, close 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 lab4.x.out, where x is the problem number.

  1. (15) Open lab4.1.py. Write a function called sumList that takes a list of numbers as a parameter and returns the sum of the numbers in the list. (If the list has 0 elements then the function should return 0.)

    After defining the function, test the function by calling the function several times, with a different list every time the function is called, and display the result of calling the function.

  2. (15 pts) Copy your lab3/lab3.4.py file into this directory as lab4.2.py. Refactor your code such that the code that displays the first 15 Fibonacci numbers is in a function called displayFibonacciSequence. Follow the instructions to refactor your code from the in-class handout (e.g., putting the rest of your code in a main function and calling the main() function at the bottom of your program, writing an appropriate comment for your function).
  3. (15 pts) Copy the previous program and name it lab4.3.py. Modify the function displayFibonacciSequence so that it takes as a parameter the number of Fibonacci numbers to display. Modify the main function to prompt a user for the number of Fibonacci sequence numbers to display and pass that value into the function.
  4. (25 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

    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?)

    Your program should use a function that takes as parameters the number of hydrogen, carbon, and oxygen molecules and returns the molecular weight, rounded to 3 decimal places.

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

    A sample run is shown below:

    This program computes the molecular weight for a molecule.
        
    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
    
  5. Finishing up: What to turn in for this lab

    1. IDLE and jEdit may create backup files with the "~" extension. Delete these files from your lab directory to save paper when you print.
    2. Copy your lab4 directory into your turnin directory.
    3. Perform the following steps from your cs111 directory.

      Note that each command below links to a page with more information about using the command.

    4. Create the printable lab assignment, using the createPrintableLab command:
      createPrintableLab <labdirname>
    5. View your file using the evince & command.
    6. Print the file using the lpr command.
    7. Log out of your machine when you are done.

    You should hand in the printed copy and submit the electronic version before the beginning of Wednesday's class.

    Grading (70 pts)