So far, I’ve walked you through writing programs step-by-step. Starting today, you’re going to have more room for experimentation. It’s time to put on your critical thinking cap!

Going off-script means the whole process is going to be slower, you’re going to make more mistakes, and you might get frustrated. That’s okay! Making mistakes is an important part of learning, and in the end you’ll have a program that is uniquely yours.

So how do you go about this, anyways?

 

Outlining a Program

There are a few different ways to plan a program. I’ll teach you my favorite approach. You start with a description of what your program needs to do, then break this into major steps, then break it down further and further until each step represents a single line of code.

Another way to think about this is that you first write your main() function, and then figure out the smaller functions that make it up.

Let’s look at an example. Say I want to write a program that will take a user’s name and age and print out a text-based “nametag” in a nice frame. So I need to:

  • get input from the user,
  • format the input, and
  • print the nametag.

As a main() function, that might look something like this:

def main():
    name, age = getInput()
    name = formatName(name)
    printNametag(name, age)

Once we’ve got the basic outline, we take a look at the individual functions for each step. Let’s start with getInput(). This function will:

  • get the user’s name,
  • get the user’s age, and
  • return these values.

Next, the formatName() function, which takes name as a parameter, will format the name. You can use a string method, .title(), to make sure that the first and last name are capitalized, then return the formatted name.

Last, the printNametag() function takes name and age and prints an ASCII “nametag”. For this function, we might:

  • define a variable for the top and bottom border,
  • define a variable to add some space above and below the name and age, and
  • print the border, spacer, name, age, spacer, and border again.

You can check out my version of the program below. This is a relatively simple program: the ones you’ve written so far are more complicated than this. But every program will have a similar basic structure.

Look at the Repl below to see an outline for what most of your programs will look like. (Pro tip: any time you write a program, for this class or on your own, you can copy this code to use as a starting point!)

  • title, name, and a description of what the program does
  • setup: import libraries (e.g. random), define constants, etc.
  • main function: definition of the main() function, which calls the rest
  • functions: define your functions
  • run the program: call main() at the very bottom

Pseudocode

Pseudocode is a great tool for outlining and planning a program. Pseudocode means “fake code”—it’s just regular English, but it’s specific enough that it’s easy to translate into actual lines of code.

For example, some pseudocode to calculate and display the area of a rectangle:

Get the length, l, and width, w
Compute the area = l*w
Display the area

To translate this pseudocode into Python, you could say something like:

l, w = input("Enter the length and width of the rectangle, separated by a comma: ").split(",")
area = int(l) * int(w)
print(area)

(The first line of this code uses a fun little shortcut called simultaneous assignment, where you type several variable names separated by commas, then list the values the same way in the same order. For example, a, b, c = 1, 3, 6 results in a having a value of 1, b having a value of 3, and c having a value of 6.)

Writing your program in pseudocode is an excellent step to take between outlining your program and actually coding it. If you’re careful and detailed and specific when you write your pseudocode, you’ll be able to make sense of your program, line-by-line, before you actually code it.

We're going to play a game to practice your programming skills: instruct me how to make a peanut butter sandwich. You can define your own functions and variables, you can "undo" an action if it doesn't work, and you can always ask me (the real me, not sandwich-making-robot-me) for help. Make sure someone's taking notes, so that you can remember what worked (and what didn't!).

Now, use the notes to write pseudocode for making a peanut butter sandwich. Remember: make sure your pseudocode is detailed and specific!

 

Writing a Program

Here’s a summary of the steps I’ve described for planning and writing a program:

  1. Start with a description of what your program will do.
  2. Break the description into major steps.
  3. Turn the outline from step 2 into a main() function that calls other functions.
  4. Determine what setup you’ll need to do (importing, defining constants, etc.)—you can always add to this later.
  5. Describe what the first function will do, then break it down into steps. You might need to create additional sub-functions at this point!
  6. Repeat step 5 for each function in your program.
  7. Turn the steps for each function into pseudocode.
  8. Translate your pseudocode into Python.
  9. Debugging time!