Code Structure

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


Imports go first        ----->


Then come the     ------>
function 
definitions.

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



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




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



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)
    1800.9435055069168
    >>> futval(250,10,.06)
    447.71192413571362
    >>> 

    """

    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
    doctest.testmod()
    main()





Comments