Python语言程序设计(美-梁勇)第5章习题解答(英文)

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

Chapter5Loops1.count100isalwaysTrueatPointA.count100isalwaysFalseatPointC.count100issometimesTrueorsometimesFalseatPointB.2.Itwouldbewrongifitisinitializedtoavaluebetween0and100,becauseitcouldbethenumberyouattempttoguess.Whentheinitialguessvalueandrandomnumberareequal,theloopwillneverbeexecuted.3.(a)Infinitenumberoftimes.(b)Infinitenumberoftimes.(c)Theloopbodyisexecutedninetimes.Theprintoutis2,4,6,8onseparatelines.4.(a)and(b)areinfiniteloops,(c)hasanindentationerror.5.maxis5number06.sumis14countis47.Yes.Theadvantagesofforloopsaresimplicityandreadability.Compilerscanproducemoreefficientcodefortheforloopthanforthecorrespondingwhileloop.8.whileloop:sum=0i=0whilei=1000:sum+=ii+=19.Canyoualwaysconvertawhileloopintoaforloop?NotinPython.Forexample,youcannotconvertthewhileloopinListing5.3,GuessNumber.py,toaforloop.sum=0foriinrange(1,10000):ifsum10000:sum=sum+i10.(A)ntimes(B)ntimes(C)n-5times(D)Theceilingof(n-5)/3times11.Tipfortracingprograms:Drawatabletoseehowvariableschangeintheprogram.Consider(a)forexample.ijoutput10011200211223003113223340041142243344(A).0010120123(B).********2****32****432****(C).1xxx2xxx4xxx8xxx16xxx1xxx2xxx4xxx8xxx1xxx2xxx4xxx1xxx2xxx1xxx(D).1G1G3G1G3G5G1G3G5G7G1G3G5G7G9G12.No.Tryn1=3andn2=3.13.Thekeywordbreakisusedtoexitthecurrentloop.Theprogramin(A)willterminate.TheoutputisBalanceis1.Thekeywordcontinuecausestherestoftheloopbodytobeskippedforthecurrentiteration.Thewhileloopwillnotterminatein(B).14.Ifacontinuestatementisexecutedinsideaforloop,therestoftheiterationisskipped,thentheaction-after-each-iterationisperformedandtheloop-continuation-conditionischecked.Ifacontinuestatementisexecutedinsideawhileloop,therestoftheiterationisskipped,thentheloop-continuation-conditionischecked.Hereisthefix:i=0whilei4:ifi%3==0:i+=1continuesum+=ii+=115.TestBreak.pysum=0number=0whilenumber20andsum100:number+=1sum+=numberprint(Thenumberis+str(number))print(Thesumis+str(sum))TestContinue.pysum=0number=0while(number20):number+=1if(number!=10andnumber!=11):sum+=numberprint(Thesumis+str(sum))16.(A)print(j)121223(B)forjinrange(1,4):121223

1 / 4
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功