Programming‎ > ‎Python Resources‎ > ‎

Code Structure

Here's how I want your code structured in the class.

Imports go first        ----->

Then come the     ------>

Use docstrings        ----->
to document your
functions and explain 
your variables.

Use doctests           ----->
to make sure
your functions
pass basic
sanity tests

your program        ----->
should always 
have a main()
function that 
contains the core

Always use         ----->
this clause at
the end of your 
code. It allows 
Doctest to run
and then calls
main() program.

import math

def futval(prin, period, apr):

    A function to compute the value of an investment
    carried years into the future.

    prin = starting principle
    period = years to compound
    apr = annual percentage rate

    >>> futval(1000,15,.04)
    >>> futval(250,10,.06)


    for i in range(period):
        prin = prin * (1 + apr)
    return prin

def main():

    p = input "Starting Principle? "
    per = input "Years for annual compounding? "
    apr = input "Annual percentage rate? "

    print futval(p, per, apr)

if __name__ == "__main__":
    import doctest