您好,欢迎访问三七文档
当前位置:首页 > 临时分类 > 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
本文标题:Python语言程序设计(美-梁勇)第5章习题解答(英文)
链接地址:https://www.777doc.com/doc-5006490 .html