Python Programming: An Introduction to Computer Science
8.5.OTHERCOMMONSTRUCTURES 133 no yes Process the item Item is the sentinel? Get next Data item Figure8.3:Loop-and-a-halfimplemen ...
134 CHAPTER8. CONTROLSTRUCTURES,PART 2 response[0] == "y" or "Y" Treatedasa Booleanexpression,thiswillalwaysevaluatetotrue.Thefi ...
8.6.EXERCISES 135 (a)Definiteloopvs.Indefiniteloop (b)Forloopvs.Whileloop (c)Interactive loopvs.Sentinelloop (d)Sentinelloopvs.E ...
136 CHAPTER8. CONTROLSTRUCTURES,PART 2 A postive wholenumbern 2 is primeif nonumberbetween2 and n(inclusive)evenlydividesn. Wr ...
Chapter 9 Simulation and Design Youmaynotrealizeit,butyouhave reacheda significantmilestoneinthejourney tobecominga computer sci ...
138 CHAPTER9. SIMULATIONANDDESIGN 9.1.2 ProgramSpecification Racquetballis a sportplayedbetweentwo playersusingracquetstostrike ...
9.2.RANDOMNUMBERS 139 thefunctiontoproducea newnumber. Witha carefullychosenfunction,theresultingsequenceofvalues looksessential ...
140 CHAPTER9. SIMULATIONANDDESIGN Thenameofthemodule(random) is thesameasthenameofthefunction,whichgivesrisetothefunny- lookingi ...
9.3.TOP-DOWNDESIGN 141 Doyouseehow thisworks.I’mjustassumingthereis aprintInstructionsfunctionthattakescareof printingtheinstruc ...
142 CHAPTER9. SIMULATIONANDDESIGN probabilities,it musthandbackthecorrectnumberofwinsforeachplayer. Themainfunctiononlycares wha ...
9.3.TOP-DOWNDESIGN 143 9.3.4 DesigningsimNGames. Now thatwearegettingsomeexperiencewiththetop-downdesigntechnique,wearereadyto t ...
144 CHAPTER9. SIMULATIONANDDESIGN probA probB n probA probB n winsAwinsB winsA winsB simOneGame probA probB scoreA scoreB main p ...
9.3.TOP-DOWNDESIGN 145 numberofpossibilitiesforthisconditioninthepreviouschapter, someofwhichwerefairlycomplex.Let’s hidethedeta ...
146 CHAPTER9. SIMULATIONANDDESIGN else: # A loses serve serving = "B" else: if random() < probB: # B wins the serve scoreB = ...
9.3.TOP-DOWNDESIGN 147 def printIntro(): print "This programsimulates a game of racquetball betweentwo" print ’playerscalled "A" ...
148 CHAPTER9. SIMULATIONANDDESIGN n = winsA + winsB print "\nGamessimulated:", n print "Wins for A: %d (%0.1f%%)"% (winsA, float ...
9.4.BOTTOM-UPIMPLEMENTATION 149 rball1.gameOver(3,15) 1 I have selectedtestdatathatexercisealltheimportantcasesforthefunct ...
150 CHAPTER9. SIMULATIONANDDESIGN has the first serve. What is the prob. playerA wins a serve? .65 What is the prob. playerB win ...
9.5.OTHERDESIGNTECHNIQUES 151 if name == ’main’:simOneGame() YoucanseethatI have addeda printstatementat thebottomoftheloop.Prin ...
152 CHAPTER9. SIMULATIONANDDESIGN 9.6 Exercises Draw thetoplevelsofa structurechartfora programhavingthefollowingmainfunction. ...
«
3
4
5
6
7
8
9
10
11
12
»
Free download pdf