# futval_graph4.py
# Plots an investment 10 years into the future.
# Illustrates use of a functions to eliminate code duplication
# and improve program readability.
#
# from chapter 6 of Zelle's Python text,
# with a few minor modifications by Jim M
from graphics import *
def createLabeledWindow():
window = GraphWin("Investment Growth Chart", 320, 240)
window.setBackground("white")
window.setCoords(-1.75, -200, 11.5, 10400)
Text(Point(-1, 0), ' 0.0K').draw(window)
Text(Point(-1, 2500), ' 2.5K').draw(window)
Text(Point(-1, 5000), ' 5.0K').draw(window)
Text(Point(-1, 7500), ' 7.5k').draw(window)
Text(Point(-1, 10000), '10.0K').draw(window)
return window
def drawBar(window, year, height):
bar = Rectangle(Point(year, 0), Point(year+1, height))
bar.setFill("green")
bar.setWidth(2)
bar.draw(window)
def main():
print "This program plots the growth of a 10 year investment."
principal = input("Enter the initial principal (e.g. 1000) : ")
apr = input("Enter the annualized interest rate (e.g. 0.25) : ")
win = createLabeledWindow()
drawBar(win, 0, principal)
for year in range(1, 11):
principal = principal * (1 + apr)
drawBar(win, year, principal)
raw_input("Press <Enter> to quit.")
win.close()
main()
syntax highlighted by Code2HTML, v. 0.93pm6