Skip to main content

Section 5.12 Pseudocode

With the introduction of conditionals and branching, our programs are about to become more complex on average. This is a good time to introduce a technique that will help you plan and structure your programs before you start writing the more complex code. That technique is to use pseudocode.
Pseudocode is a mix of English and code (in our case, Python) that programmers use to plan out their programs.
Writing pseudocode can be helpful for reasons such as:
  • It’s quicker to write than actual code!
  • It’s readable to more people than actual code, and can still be critiqued like regular code.
  • It can help you to take on a large or confusing programming task.
  • It can help you to document your code with comments for yourself and others’ readability.
There isn’t necessarily a right or wrong syntax for pseudocode (except wrong would be if you just wrote the program!). Your pseudocode and language will probably look different from this, but should be along the same lines.
Take the following prompt for a problem:
NUMERATOR/DENOMINATOR
We can approach this problem first by writing the following pseudocode:
take user input for fraction
split the user input by "/"
make a variable for numerator, type convert to int
make a variable for denominator, type convert to int
if the denominator is 0,
    print an error message
otherwise
    make a variable for the numerator divided by denominator
    make a variable for the numerator mod denominator
    print out both variables
As you can see, this is basically just a high level outline of your program. It may help you better see the control flow of your program, or even remind you to do the small things, like converting your string input to an integer if necessary.
This pseudocode can then be more readily converted to Python:
fraction = input().split('/')
numerator = int(fraction[0])
denominator = int(fraction[1])
if denominator == 0:
    print("Error, your denominator is 0!")
else:
    result = numerator / denominator
    remainder = numerator % denominator
    print("Division:", result)
    print("Remainder:", remainder)
..and we’re done! That’s the pseudocode process.
This won’t be something you see often, but let’s reverse engineer and see what existing Python code might look like in pseudocode.
character = input()
if character == "Yoda":
    print("No! Try not. Do. Or do not. There is no try.")
elif character == "Han Solo":
    print("Never tell me the odds!")
elif character == "Obi Wan Kenobi":
    print("Use the Force, Luke.")
else:
    print("Character not found!")
We could write the following pseudocode for this program:
take user input for a Star Wars character
if the character is Yoda, print "No! Try not. Do. Or do not. There is no try."
if the character is Han Solo, print "Never tell me the odds!"
if the character is Obi Wan Kenobi, print "Use the Force, Luke."
in all other cases, print that the character wasn't found
As you may start to notice, Python is so close to the English language that pseudocode can often be quickly translated into code.
Feel free to use pseudocode on exams (for planning before writing code) and in your actual programs.
You have attempted of activities on this page.