Python程序设计(第三版) 课后题答案 第一章 [约翰·策勒 (著) 人民邮电出版社]

Python程序设计(第三版)课后题答案 第一章

编程练习题

  • 1.启动交互式Python会话,并尝试键入以下每个命令。写出你看到的结果。
    a. print(“Hello,world!”)
    b. print(“Hello”,“world!”)
    c. print(3)
    d. print(3.0)
    e. print(2 + 3)
    f. print(2.0 + 3.0)
    g. print(“2” + “3”)
    h. print(“2 + 3 =”, 2+3)
    i. print(2 * 3)
    j. print(2 ** 3)
    k. print(7 / 3)
    l. print(7 // 3)

答案:程序如下:

print("Hello,world!")
print("Hello","world!")
print(3)
print(3.0)
print(2 + 3)
print(2.0 + 3.0)
print("2" + "3")
print("2 + 3 =", 2+3)
print(2 * 3)
print(2 ** 3)
print(7 / 3)
print(7 // 3)

输出结果如下:

  • 2.运行下列chaos程序。尝试使用各种输入值,观察它在本章中描述的功能。
def main():
    print("This program illustrates a chaotic function")
    x = eval(input("Enter a number between 0 and 1: "))
    for i in range(10):
        x = 3.9 * x * (1 - x)
        print(x)


main()

答案:输入0.26时,输出如下:

==
This program illustrates a chaotic function
Enter a number between 0 and 1: 0.26
0.75036
0.73054749456
0.7677066257332165
0.6954993339002887
0.8259420407337192
0.5606709657211202
0.9606442322820199
0.14744687593470315
0.49025454937601765
0.9746296021493285
>>>
  • 3.修改chaos程序,使用2.0代替3.9作为逻辑函数中的乘数。修改代码行如下:x = 2.0 * x * (1 - x)
    运行程序并输入不同的值观察输出,描述与上一题中的差异。
    答案:输出如下:可见与上题中的输出结果不同,后面几次的运算结果不变。
==
This program illustrates a chaotic function
Enter a number between 0 and 1: 0.26
0.38480000000000003
0.47345792000000003
0.49859103597854726
0.4999960296407725
0.4999999999684725
0.49999999999999994
0.49999999999999994
0.49999999999999994
0.49999999999999994
0.49999999999999994
>>> 
  • 4.修改chaos程序,让它打印出20个值,而不是10个。
    答案:修改程序语句:for i in range(20):
    仍然输入0.26后输出结果如下:
==
This program illustrates a chaotic function
Enter a number between 0 and 1: 0.26
0.38480000000000003
0.47345792000000003
0.49859103597854726
0.4999960296407725
0.4999999999684725
0.49999999999999994
0.49999999999999994
0.49999999999999994
0.49999999999999994
0.49999999999999994
0.49999999999999994
0.49999999999999994
0.49999999999999994
0.49999999999999994
0.49999999999999994
0.49999999999999994
0.49999999999999994
0.49999999999999994
0.49999999999999994
0.49999999999999994
>>> 
  • 5.修改chaos程序,让打印值的数量由用户决定,在程序顶端添加一行代码如下:
    n = eval(input("How many numbers should I print?:")),更改循环,使用n代替具体的数字。
    答案
def main():
    print("This program illustrates a chaotic function")
    n = eval(input("How many numbers should I print?:"))
    x = eval(input("Enter a number between 0 and 1: "))
    for i in range(n):
        x = 2.0 * x * (1 - x)
        print(x)


main()

输入n = 5和x = 0.26,输出结果如下:

==
This program illustrates a chaotic function
How many numbers should I print?:5
Enter a number between 0 and 1: 0.26
0.38480000000000003
0.47345792000000003
0.49859103597854726
0.4999960296407725
0.4999999999684725
>>> 
  • 6.在chaos程序中执行的计算,可以用代数等价的多种方式来编写。为以下每种计算方式编写一个程序版本。让你修改的程序打印出100次迭代的计算,并比较相同输入的运算结果。
    a. 3.9 * x * (1 - x)
    b. 3.9 * ( x - x * x)
    c. 3.9 * x - 3.9 * x * x
    请解释实验的结果。
    答案
    a式:
def main():
    print("This program illustrates a chaotic function")
    n = eval(input("How many numbers should I print?:"))
    x = eval(input("Enter a number between 0 and 1: "))
    for i in range(n):
        x = 3.9 * x * (1 - x)
        print(x)


main()

输出为:

==
This program illustrates a chaotic function
How many numbers should I print?:100
Enter a number between 0 and 1: 0.25
0.73125
0.76644140625
0.6981350104385375
0.8218958187902304
0.5708940191969317
0.9553987483642099
0.166186721954413
0.5404179120617926
0.9686289302998042
0.11850901017563877
0.4074120362630336
0.9415671289870646
0.214572035332672
0.6572704202448796
0.8785374581723959
0.4161666317654883
0.9475906688447814
0.19368411333601687
0.6090652525513056
0.9286086056750876
0.25854918625090323
0.747635867705606
0.7358382604001973
0.7580832282324941
0.7152328844898681
0.7943317411932672
0.6371384218919443
0.9016529076398497
0.3458322729593719
0.8823060165625929
0.4049842278301656
0.9397908118519834
0.2206777630612359
0.6707184430109472
0.8613353315420801
0.4658034348995627
0.9704393202477929
0.11187869926269879
0.387511238064543
0.9256504859090895
0.2684044889968884
0.7658177252045096
0.6994296541716892
0.8198884708441456
0.5759183282618774
0.952521988992295
0.1763730129659832
0.5665357357268088
0.9577346838980398
0.15786794071137003
0.5184880506269934
0.9736669487376536
0.09999452452688812
0.35098291640696583
0.8883962443099641
0.38667859388306725
0.9249172097711567
0.2708369228771499
0.7701887079259878
0.6902924422262124
0.8337762670831761
0.5405142737748792
0.9685985151199271
0.11862018334685781
0.40774279825350096
0.9418055740310326
0.2137505555449281
0.6554388966417345
0.8807711227021208
0.4095520732530493
0.9430947729339951
0.20930138657442807
0.6454278329945545
0.8925179070229811
0.3741258013985637
0.91320717589314
0.30911333618262093
0.8328928981490166
0.5428110416115991
0.9678521373929067
0.1213460723938663
0.41582269212295053
0.9473653052704324
0.19447070619714207
0.6109422174504751
0.9269981151099689
0.26392317780088165
0.7576441626794268
0.7161159932051465
0.7928461221757169
0.6405404800339158
0.8979686565401623
0.3573216988053114
0.8956073192359711
0.3646299109710208
0.9035322620855163
0.3399306824789701
0.8750734729945717
0.4263475704375821
0.953843746516257
>>> 

b式:

def main():
    print("This program illustrates a chaotic function")
    n = eval(input("How many numbers should I print?:"))
    x = eval(input("Enter a number between 0 and 1: "))
    for i in range(n):
        x = 3.9 *  ( x - x *  x)
        print(x)


main()

输出为:

==
This program illustrates a chaotic function
How many numbers should I print?:100
Enter a number between 0 and 1: 0.25
0.73125
0.76644140625
0.6981350104385374
0.8218958187902304
0.5708940191969316
0.9553987483642099
0.16618672195441295
0.5404179120617923
0.9686289302998042
0.11850901017563896
0.40741203626303407
0.9415671289870648
0.2145720353326712
0.6572704202448778
0.878537458172398
0.4161666317654822
0.9475906688447774
0.19368411333603094
0.6090652525513391
0.9286086056750592
0.2585491862509983
0.747635867705785
0.7358382603998516
0.7580832282331301
0.7152328844885875
0.7943317411954173
0.6371384218870082
0.9016529076451297
0.3458322729428301
0.8823060165427012
0.4049842278894823
0.9397908118959444
0.22067776291043387
0.6707184426823929
0.8613353319795843
0.46580343366649407
0.9704393199188927
0.11187870046957413
0.3875112417181724
0.925650489114829
0.26840447835359665
0.7658177059779883
0.6994296940355393
0.8198884088338755
0.575918482985552
0.9525218973705725
0.17637333636053473
0.5665365520681948
0.9577342602316181
0.15786945333983177
0.5184920872638563
0.973666366563641
0.09999667542695391
0.3509896272889903
0.8884040444156514
0.38665496329810245
0.9248963203546774
0.27090615610131596
0.770312441682129
0.6900316171001978
0.8341631395589068
0.5395064850245248
0.9689130267999273
0.1174702358595895
0.40431682023205945
0.9392944435270343
0.2223795283564658
0.6744148075252443
0.8563599521721348
0.479729579502708
0.9733975292061655
0.1009896394318408
0.35408385542114357
0.8919630671298447
0.3758233206240429
0.9148625939667269
0.3037672096960748
0.8248214988371892
0.5635148761833314
0.9592668559633226
0.15238842455290233
0.5037481511992282
0.9749452103140921
0.09526548407884088
0.33614088932605346
0.8702857482116589
0.44026501221819464
0.9610837518153595
0.14586691786630052
0.48590006454009715
0.9742246480981036
0.09793283423129635
0.34453377742181496
0.8807379891853538
0.40965047600526433
0.9431641577043063
0.20906156437230772
0.6448838240305935
0.8931338423836608
0.37223854969336995
>>> 

c式:

def main():
    print("This program illustrates a chaotic function")
    n = eval(input("How many numbers should I print?:"))
    x = eval(input("Enter a number between 0 and 1: "))
    for i in range(n):
        x = 3.9 * x - 3.9 * x  *  x
        print(x)


main()

输出为:

==
This program illustrates a chaotic function
How many numbers should I print?:100
Enter a number between 0 and 1: 0.25
0.73125
0.7664414062500002
0.6981350104385373
0.8218958187902308
0.5708940191969307
0.9553987483642103
0.16618672195441153
0.5404179120617887
0.9686289302998055
0.11850901017563409
0.4074120362630196
0.9415671289870544
0.2145720353327074
0.6572704202449584
0.8785374581722993
0.4161666317657735
0.9475906688449679
0.19368411333536573
0.6090652525497497
0.9286086056764111
0.2585491862464786
0.747635867697273
0.7358382604162932
0.7580832282028851
0.7152328845494722
0.7943317410932029
0.6371384221216707
0.901652907394116
0.34583227372922964
0.8823060174883532
0.4049842250695601
0.9397908098060351
0.22067777007959233
0.6707184583019346
0.8613353111805415
0.4658034922868386
0.9704393355548724
0.1118786430944918
0.38751106802392576
0.9256503367132403
0.26840498433803006
0.7658186200100743
0.699427798898165
0.819891356803869
0.5759111273845008
0.952526252882935
0.17635796276174664
0.5664977437558647
0.9577543952939795
0.15779756299726833
0.5183002192266267
0.9736938977074039
0.09989495597159337
0.3506722195978045
0.8880347346005992
0.387773274499426
0.9258801321253334
0.2676418409375434
0.7644377750767137
0.7022833861386615
0.8154175835998809
0.5869958170284475
0.9454837384962562
0.20102253106805668
0.6263886449770906
0.9127010506413342
0.3107435869182016
0.8353098394189055
0.5365125151965846
0.9698006613126748
0.11422062065767635
0.3945796548495811
0.9316575482306295
0.24831986811653461
0.7279627337389738
0.7723286689023894
0.685764674766026
0.840416793875383
0.5230539851456593
0.9729272036987247
0.10272545400664956
0.3594744469125314
0.8979850188248981
0.35727090668468753
0.8955507830934164
0.3648043541773216
0.9037163356672967
0.33935116922994535
0.8743486173716358
0.42846613942058376
0.955043336483358
0.16744869149616415
0.5436975461283077
0.9675530454032213
0.12243718396327585
0.41904064779103334
0.9494377748306275
0.1872221775656473
0.5934631317935757
0.9409321076818131
>>> 

结论: 根据以上结果可知,当迭代次数较小时,计算输出的数值是一样的;当次数增多后,由于不同类型运算之间的小数位数保留值不统一,导致了最终的计算值不一致。

  • 7.修改chaos程序,让他接受两个输入,然后打印一个包含两列的表。
    答案: 输入程序如下:
import math
def main():
    print("This program illustrates a chaotic function")
    n = eval(input("How many numbers should I print?:"))
    x1 = eval(input("Enter a number between 0 and 1: x1 = "))
    x2 = eval(input("Enter a number between 0 and 1: x2 = "))
    print("x1=",x1," ""x2=",x2)
    for i in range(n):
        x1 = 3.9 * x1 - 3.9 * x1  *  x1
        x2 = 3.9 * x2 - 3.9 * x2  *  x2
        a=round(x1,4)
        b=round(x2,4)
       
        print("",a," ", b)


main()

输出为:

==
This program illustrates a chaotic function
How many numbers should I print?:5
Enter a number between 0 and 1: x1 = 0.25
Enter a number between 0 and 1: x2 = 0.26
x1= 0.25  x2= 0.26
 0.7312   0.7504
 0.7664   0.7305
 0.6981   0.7677
 0.8219   0.6955
 0.5709   0.8259
>>>
  • 17
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值