Design Process

  1. Show you understand the problem.
  2. Write using a fast feedback process.
  3. Demonstrate your code works.

Understand the problem

Examples that you have worked out show that you understand the problem. Pick two examples and show:

  1. What information is input to the function?
  2. What is the output from the function?

If the problem involves a process, you should demonstrate the process working in some step-by-step manner.

Fast feedback

One of the biggest problems beginner face is they hold incorrect beliefs about what the computer does at some point. Several steps later, the unexpected output causes an error. Confusion ensues.

  • Write code that runs immediately, not functions.
  • Use variables to hold every result along the way.
  • Print out each result to make sure you know what happens!

Example:

step1 = file.readline()
step2 = step1.strip().split()
print(step2)
if step2[0] == 'NVERT':
  step3 = step2[1]
else:
  step3 = -1
print(step3)

When you have a working example, then change it into a function. Not before!

Demonstrate code works

Complex code that has not been tested does not work. Bugs are a fact of life. You must perform some testing of your code to demonstrate that it functions the way you want. Using the example that you used to show you understood the problem is fine.

Example:

import unittest

class DemoTest(unittest.TestCase):
    def test_addition(self):
        self.assertEqual(1+2,3)
if __name__ == '__main__':
    unittest.main()
Last modified August 28, 2023: Python introduction content days 1-4. (75c2bfd)