Python Programming: An Introduction to Computer Science

(Nora) #1
9.3.TOP-DOWNDESIGN 141

Doyouseehow thisworks.I’mjustassumingthereis aprintInstructionsfunctionthattakescareof
printingtheinstructions.Thatstepwaseasy!Let’s move on.
Next,I needtogetsomeinputsfromtheuser. I alsoknowhowtodothat—Ijustneeda fewinput
statements.Again,thatdoesn’t seemveryinteresting,andI feellike puttingoff thedetails.Let’s assumethat
a componentalreadyexiststosolve thatproblem.We’ll callthefunctiongetInputs. Thepointofthis
functionis togetvaluesforvariablesprobA,probBandn. Thefunctionmustreturnthesevaluesforthe
mainprogramtouse.Hereis ourprogramsofar:


def main():
printInstructions()
probA, probB, n = getInputs()


We’remakingprogress,let’s move ontothenextline.
Herewe’ve hitthecruxoftheproblem.We needtosimulatengamesofracquetballusingthevaluesof
probA, andprobB. Thistime,I reallydon’t have a verygoodideahowthatwillevenbeaccomplished.
Let’s procrastinateagainandpushthedetailsoff intoa function.(Maybewecangetsomeoneelsetowrite
thatpartforuslater.) Butwhatshouldweputintomain? Let’s callourfunctionsimNGames. We needto
figureoutwhatthecallofthisfunctionlookslike.
Supposeyouwereaskinga friendtoactuallycarryouta simulationofngames.Whatinformationwould
youhave togive him?Yourfriendwouldneedtoknow how many gameshewassupposedtosimulateand
whatthevaluesofprobAandprobBshouldbeforthosesimulations.Thesethreevalueswill,ina sense,
beinputstothefunction.
Whatinformationdoyouneedtogetbackfromyourfriend? Well,inordertofinishouttheprogram
(printa report)youneedto know how many gameswerewonbyplayerA andhow many gameswerewonby
PlayerB.ThesemustbeoutputsfromthesimNGamesfunction.Rememberinthediscussionoffunctions
inChapter6, I saidthatparameterswereusedasfunctioninputs,andreturnvaluesserve asfunctionoutputs.
Giventhisanalysis,wenow know how thenextstepofthealgorithmcanbecoded.


def main():
printInstructions()
probA, probB, n = getInputs()
winsA, winsB = simNGames(n,probA, probB)


Areyougettingthehangofthis?Thelaststepis toprinta report.If youtoldyourfriendtotypeupthe
report,youwouldhave totellhimhow many winstherewereforeachplayer;thesevaluesareinputstothe
function.Here’s thecompleteprogram.


def main():
printInstructions()
probA, probB, n = getInputs()
winsA, winsB = simNGames(n,probA, probB)
printSummary(winsA,winsB)


Thatwasn’t veryhard.Themainfunctionis onlyfive lineslong,andtheprogramlookslike a moreprecise
formulationoftheroughalgorithm.


9.3.2 SeparationofConcerns.


Ofcourse,themainfunctionalonewon’t doverymuch;we’ve putoff alloftheinterestingdetails.Infact,
youmaythinkthatwehave notyetaccomplishedanythingat all,butthatis farfromtrue.
We have brokentheoriginalproblemintofourindependenttasks:printInstructions,getInputs,
simNGamesandprintSummary. Further, wehave specifiedthename,parametersandexpectedreturn
valuesofthefunctionsthatperformthesetasks. Thisinformationis calledtheinterfaceorsignatureofa
function.
Havingsignaturesallowsusto tacklepiecesindependently. Forthepurposesofmain, wedon’t carehow
simNGamesdoesitsjob. Theonlyconcernis that,whengiventhenumberofgamesto simulateandthetwo

Free download pdf