Lab 3
Help!

Objectives

Preliminary Setup

  1. Create a new PyCharm project. Remember to use your name in the project name (and avoid spaces in the project name).
  2. Download the starter code and an input file:
  3. Copy badmain.py to goodmain.py. You'll be making changes to goodmain.py so that it is improved over badmain.py.

Have a look around

Now that you have a project and some code, explore to see what you have:

  1. Skim the code. There is some main-line code and several helper functions. Note that some of the for loops use Python's enumerate function. See here for info.

    Note: don't let yourself get lost in the parts you don't understand.

  2. Run the code. What happens? Make sure you put the input file in the right place.

Fix it

The main task of this lab is to improve the helper functions (and the code that calls them) in several ways. By the end of the lab, goodmain should do all that badmain does, but it should be better written.

In all of the following steps, change goodmain while leaving badmain unchanged.

Step 1: Add appropriate documentation

  1. For each of the functions, add a docstring immediately after the function signature. Recall that a Python docstring should be of the form:
    does this, returns that

    In addition to the short description, if the function has parameters, or returns something, you should have :param: and :return: tags, respectively.

    Make sure each of your docstrings explains the behavior (i.e. what the function does), not the implementation (i.e. how it does it).

  2. Make sure that goodmain still gets the same answers as badmain.

Step 2: Remove uses of global variables.

  1. In each of the functions, carefully look at each use of a variable. Determine if the variable used is one of the parameters to the function, a local variable, or a variable defined in the global scope. For any function that uses globals, re-write the function so that it no longer uses globals:

    Note: don't just search for the 'global' keyword. Functions can still use global variables without those variables being explicitly marked as 'global'.

  2. Make sure that goodmain still gets the same answers as badmain.

Step 3: Move the main-line code

  1. Create a main function that contains the main-line code.
  2. Make sure that goodmain still gets the same answers as badmain.

Step 4: [Extra Credit] Abstract some helpers

  1. Several of the helpers all have the same logic. Improve the code by deciding which helpers can be combined and then replacing them with only one. You'll need to change the calls to those functions.
  2. Make sure that goodmain still gets the same answers as badmain.

How to turn in this lab

Before turning in any program in this class, remember this mantra:

Just because it works doesn't mean it's good.

Part of your grade will also come from things like how understandable and readable your code is. You will also be graded on the neatness, presentation, and style of your program code.

For all labs, turn in only an electronic version. Please compress your program and email the tarball or zip file to me at cassa@union.edu.

Ask for help if you're having problems!