《Python语言程序设计 》第5章编程题答案(5.1-5.16)

5
回复
1198
查看
[复制链接]
  • TA的每日心情
    开心
    2020-1-15 12:08
  • 签到天数: 43 天

    [LV.5]常住居民I

    20

    主题

    66

    帖子

    376

    积分

    筑基程序员

    Rank: 3Rank: 3

    积分
    376
    发表于 2019-12-2 22:36:21 | 显示全部楼层 |阅读模式

    登录后查看本帖详细内容!

    您需要 登录 才可以下载或查看,没有帐号?立即注册

    x
    5.1:
    number=eval(input('enter an integer,the input ends if it is 0:'))
    if number==0:
    print('you didn\'t enter any number')
    else:
    sum1=0
    sum2=0
    count1=0
    count2=0
    while number!=0:
    if number > 0:
    sum1+=number
    count1+=1
    number=eval(input('enter an integer,the input ends if it is 0:'))
    else:
    sum2+=number
    count2+=1
    number=eval(input('enter an integer,the input ends if it is 0:'))
    total=sum1+sum2
    average=total/(count1+count2)
    print('the total is',total)
    print('the average is ',average)
    5.2:
    import random
    import time
    count=0
    right_count=0
    starttime=time.time()
    while count < 5:
    number1=random.randint(1,15)
    number2=random.randint(1,15)
    answer=eval(input('what is '+str(number1)+'+'+str(number2)+'?'))
    if answer==number1+number2:
    right_count+=1
    print('right!')
    else:
    print('wrong!')
    count+=1
    endtime=time.time()
    print('you are right '+str(right_count)+'in'+str(count)+'times','you spend '+str(int(endtime-starttime))+'s')
    5.3:
    print('-----------')
    print(format('公斤','^3s')+'|'+format('磅','^3s'))
    for i in range(1,20,2):
    print('-'*5+'|'+'-'*5)
    print(format(i,'^5d')+'|'+format(i*2.2,'^.1f'))
    5.4:
    print('-----------')
    print(format('英里','^3s')+'|'+format('公里','^5s'))
    for i in range(1,11):
    print('-'*5+'|'+'-'*5)
    print(format(i,'^5d')+'|'+format(i*1.609,'^.3f'))
    5.5:
    print('━'*25)
    print(format('公斤','^3s')+'|'+format('','^5s')+'||'+format('公斤','^3s')+'|'+format('','^5s'))
    j=20
    for i in range(1,20,2):
        print('━'*25)
        print(format(i,'^5d')+'|'+format(i*2.2,'^6.1f')+'||'+format(j/2.2,'^5.2f')+'|'+format(j,'^5d'))
        j+=5
    5.6:
    print('━'*25)
    print(format('英里','^3s')+'|'+format('公里','^5s')+'||'+format('英里','^3s')+'|'+format('公里','^5s'))
    j=20
    for i in range(1,20):
        print('━'*25)
        print(format(i,'^5d')+'|'+format(i*1.609,'^6.3f')+'||'+format(j/1.609,'^5.3f')+'|'+format(j,'^5d'))
        j+=5
    5.7:
    import math
    print('━'*25)
    print(format('','^4s')+'|'+format('sin','^7s')+'|'+format('cos','^7s'))
    for i in range(0,370,10):
        print('━'*25)
        print(format(i,'^5d')+'|'+format(str(round(math.sin(math.radians(i)),4)),'^7s')+\
            '|'+format(str(round(math.cos(math.radians(i)),4)),'^7s'))
    5.8:
    import math
    print('━'*15)
    print(format('Number','^4s')+'|'+format('Square Root','^7s'))
    for i in range(0,21,2):
        print('━'*15)
        print(format(i,'^5d')+'|'+format(str(round(math.sqrt(i),4)),'^7s'))
    5.9:
    fees=10000
    growth=1+0.05
    for i in range(1,11):
        fees=fees*growth
        print(''+str(i+1)+'年的学费为:'+str(round(fees)))
        fees1=fees
        fouryears_total=fees
        for j in range(3):
            fees1*=growth
            fouryears_total+=fees1
        print(''+str(i+1)+'年的四年学费:'+str(round(fouryears_total)))
    5.10:
    number=eval(input('number is '))
    print(number)
    score=eval(input('score is'))
    print(score)
    print('max score is '+str(max(score)))
    cmd中输入‘cd C:\Users\beardool\Desktop\python code_wg’,再输入‘python 测试.py<score.txt
    5.11:
    number=eval(input('number is '))
    print(number)
    score=eval(input('score is'))
    print(score)
    score=sorted(score)
    print('the max scores is '+str(score[-1])+'and'+str(score[-2]))
    定向输入与5.10一致
    5.12:
    count=0
    for i in range(100,1000):
    if i % 5==0 and i % 6==0:
    count+=1
    print(i,end=' ')
    if count % 10 ==0:
    print()
    5.13
    count=0
    for i in range(100,1000):
    if (i % 5==0 or i % 6==0) and not (i % 5==0 and i % 6==0):
    count+=1
    print(i,end=' ')
    if count % 10 ==0:
    print()
    5.14:
    n=0
    while n*n<12000:
    n+=1
    else:
    print(n)
    5.15:
    n=0
    while n*n*n<12000:
    n+=1
    else:
    print(n-1)
    5.16:
    n1=eval(input('n1:'))
    n2=eval(input('n2:'))
    if n1 > n2 :
    n1,n2=n2,n1
    d=n1
    while (n1 % d !=0) or (n2 % d != 0):
    d-=1
    print(d)

    〖下载地址失效反馈〗:

    下载地址如果失效,请反馈。反馈地址: https://www.fstcode.com/thread-5527-1-1.html

    〖赞助VIP免灵石下载全站资源〗:

    全站资源高清无密,每天更新,VIP特权: https://www.fstcode.com/plugin.php?id=threed_vip

    〖客服24小时咨询〗:

    有任何问题,请点击右侧客服QQ咨询。

    回复

    使用道具 举报

  • TA的每日心情
    奋斗
    2019-10-4 11:20
  • 签到天数: 1 天

    [LV.1]初来乍到

    0

    主题

    100

    帖子

    304

    积分

    终身VIP

    Rank: 12Rank: 12Rank: 12

    积分
    304
    发表于 2023-3-24 16:25:19 | 显示全部楼层
    锄禾日当午,发帖真辛苦。谁知坛中餐,帖帖皆辛苦!
    回复

    使用道具 举报

  • TA的每日心情
    慵懒
    2022-3-25 23:33
  • 签到天数: 16 天

    [LV.4]偶尔看看III

    0

    主题

    135

    帖子

    485

    积分

    筑基程序员

    Rank: 3Rank: 3

    积分
    485
    发表于 2023-3-25 21:25:02 | 显示全部楼层
    在撸一遍。。。
    回复

    使用道具 举报

  • TA的每日心情
    擦汗
    2021-6-20 09:30
  • 签到天数: 33 天

    [LV.5]常住居民I

    0

    主题

    126

    帖子

    550

    积分

    终身VIP

    Rank: 12Rank: 12Rank: 12

    积分
    550
    发表于 2023-3-26 11:26:46 | 显示全部楼层
    OMG!介是啥东东!!!
    回复

    使用道具 举报

  • TA的每日心情
    开心
    2021-6-21 13:13
  • 签到天数: 4 天

    [LV.2]偶尔看看I

    0

    主题

    93

    帖子

    305

    积分

    终身VIP

    Rank: 12Rank: 12Rank: 12

    积分
    305
    发表于 2023-3-26 19:00:36 | 显示全部楼层
    有空一起交流一下
    回复

    使用道具 举报

  • TA的每日心情
    无聊
    2019-9-8 23:43
  • 签到天数: 3 天

    [LV.2]偶尔看看I

    0

    主题

    78

    帖子

    252

    积分

    终身VIP

    Rank: 12Rank: 12Rank: 12

    积分
    252
    发表于 2023-3-27 20:09:06 | 显示全部楼层
    站位支持
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

     
    在线客服
    点击这里给我发消息 点击这里给我发消息
    用心服务所有程序员,做最好的编程视频网站
    QQ:354410543
    周一至周日 00:00-24:00
    联系站长:admin@fstcode.com

    QQ群(仅限付费用户)

    Powered by "真全栈程序员" © 2010-2023 "真全栈程序员" 本站资源全部来自互联网及网友分享-如有侵权请发邮件到站长邮箱联系删除!