2.7.EXAMPLEPROGRAM:FUTUREVALUE 23
$100investmentwillgrow to$103inoneyear’s time.How shouldtheuserrepresentanannualizedrateof
3%?Therearea numberofreasonablechoices.Let’s assumetheusersuppliesa decimal,sotheratewould
beenteredas0.03.
Thisleadsustothefollowingspecification.
ProgramFutureValue
Inputs
principalTheamountofmoney beinginvestedindollars.
aprTheannualizedpercentagerateexpressedasa decimalfraction.
OutputThevalueoftheinvestment 10 yearsintothefuture.
RelationshipValueafteroneyearis givenbyprincipal
1 a pr. Thisformulaneedsto beapplied 10 times.
Nextwedesignanalgorithmfortheprogram.We’ll usepseudocode,sothatwecanformulateourideas
withoutworryingaboutalltherulesofPython.Givenourspecification,thealgorithmseemsstraightforward.
Print an introduction
Input the amount of theprincipal (principal)
Input the annualizedpercentage rate (apr)
Repeat 10 times:
principal = principal* (1 + apr)
Output the value of principal
Now thatwe’ve thoughttheproblemallthewaythroughtopseudocode,it’s timetoputournew Python
knowledgetoworkanddevelopa program.Eachlineofthealgorithmtranslatesintoa statementofPython.
Printanintroduction(printstatement,Section2.4)
print "This programcalculates the future value of a 10-year investment"
Inputtheamountoftheprincipal(inputstatement,Section2.5.2)
principal = input("Enterthe initial principal: ")
Inputtheannualizedpercentagerate(inputstatement,Section2.5.2)
apr = input("Enterthe annualized interest rate: ")
Repeat 10 times:(countedloop,Section2.6)
for i in range(10):
Calculateprincipal= principal (1+ apr)(simpleassignmentstatement,Section2.5.1)
principal = principal (1 + apr)
Outputthevalueoftheprincipal(printstatement,Section2.4)
print "The amount in 10 yearsis:", principal
Allofthestatementtypesinthisprogramhave beendiscussedindetailinthischapter. Ifyouhave any
questions,youshouldgobackandreviewtherelevantdescriptions. Noticeespeciallythecountedloop
patternis usedtoapplytheinterestformula 10 times.
Thataboutwrapsit up.Hereis thecompletedprogram.