Python Programming: An Introduction to Computer Science

(Nora) #1
152 CHAPTER9. SIMULATIONANDDESIGN

9.6 Exercises



  1. Draw thetoplevelsofa structurechartfora programhavingthefollowingmainfunction.


def main():
printIntro()
length, width= getDimensions()
amtNeeded = computeAmount(length,width)
printReport(length,width, amtNeeded)


  1. Writeanexpressionusingeitherrandomorrandrangetocalculatethefollowing.
    A randomintintherange0–10
    A randomfloatintherange-0.5–0.5
    A randomnumberrepresentingtherollofa six-sideddie
    A randomnumberrepresentingthesumresultingfromrollingtwo six-sideddice
    A randomfloatintherange-10.0–10.0

  2. Revisetheracquetballsimulationsothatit keepstrackofresultsforbestofngamematches.

  3. Revisetheracquetballsimulationtotake shutoutsintoaccount.Yourupdatedversionshouldreportfor
    bothplayersthenumberofwins,percentageofwins,numberofshutouts,andpercentageofwinsthat
    areshutouts.

  4. Designandimplementa simulationofthegameofvolleyball.Normalvolleyballis playedlike rac-
    quetball,inthata teamcanonlyscorepointswhenit is serving.Gamesareplayedto15,butmustbe
    wonbyat leasttwo points.

  5. Collegevolleyballisnowplayedusingrallyscoring. Inthissystem,theteamthatwinsa rallyis
    awardeda point,evenif they werenottheservingteam.Gamesareplayedtoa scoreof21.Design
    andimplementa simulationofvolleyballusingrallyscoring.

  6. Designandimplementa systemthatcomparesregularvolleyballgamestothoseusingrallyscoring.
    Yourprogramshouldbeabletoinvestigatewhetherrallyscoringmagnifies,reduces,orhasnoeffect
    ontherelative advantageenjoyedbythebetterteam.

  7. Designandimplementa simulationofsomeotherracquetsport(e.g.tennisortabletennis).

  8. Crapsis a dicegameplayedatmany casinos.Aplayerrollsa pairofnormalsix-sideddice.If the
    initialrollis 2,3 or12,theplayerloses.If therollis 7 or11,theplayerwins.Any otherinitialroll
    causestheplayerto“rollforpoint.” Thatis,theplayerkeepsrollingthediceuntileitherrollinga 7 or
    re-rollingthevalueoftheinitialroll.If theplayerre-rollstheinitialvaluebeforerollinga 7, it’s a win.
    Rollinga 7 firstis a loss.
    Writea programtosimulatemultiplegamesofcrapsandestimatetheprobabilitythattheplayerwins.
    Forexample,if theplayerwins 249 outof 500 games,thentheestimatedprobabilityofwinningis
    249



500 0 498


  1. Blackjack(twenty-one)is a casinogameplayedwithcards.Thegoalofthegametodrawcardsthat
    totalascloseto 21 pointsaspossiblewithoutgoingover. Allfacecardscountas 10 points,acescount
    as1 or11,andallothercardscounttheirnumericvalue.
    Thegameisplayedagainsta dealer. Theplayertriestogetcloserto 21 (withoutgoingover)than
    thedealer. If thedealerbusts(goesover21)theplayerautomaticallywins(providedtheplayerhad
    notalreadybusted).Thedealermustalwaystake cardsaccordingtoa fixedsetofrules.Thedealer
    takescardsuntilheorsheachievesa totalofat least17.If thedealer’s handcontainsanace,it willbe
    countedas 11 whenthatresultsina totalbetween 17 and 21 inclusive; otherwise,theaceis countedas





Free download pdf