《Python语言程序设计 》第4章编程题答案(4)

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

    [LV.5]常住居民I

    20

    主题

    66

    帖子

    376

    积分

    筑基程序员

    Rank: 3Rank: 3

    积分
    376
    发表于 2019-11-29 21:50:37 | 显示全部楼层 |阅读模式

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

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

    x
    4.21
    from turtle import*
    import math
    x1,y1=eval(input('输入圆心的坐标:'))
    x2,y2=eval(input('输入一个点的坐标:'))
    radius=eval(input('输入圆半径:'))
    penup()
    goto(x1,y1-radius)
    pendown()
    circle(radius)
    penup()
    goto(x2,y2)
    pendown()
    dot(3,'red')
    done()
    distance=math.sqrt((x2-x1)**2+(y2-y1)**2)
    if distance < radius:
    print('point('+str(x2)+','+str(y2)+') is in thecircle')
    else:
    print('point('+str(x2)+','+str(y2)+') is not in thecircle')
    4.22
    from turtle import*
    import math
    x1,y1=eval(input('请输入圆心:'))
    x2,y2=eval(input('请输入长和宽:'))
    x3,y3=eval(input('输入一点的坐标:'))
    penup()
    goto(x1+x2/2,y1)
    pendown()
    left(90)
    forward(y2/2)
    left(90)
    forward(x2)
    left(90)
    forward(y2)
    left(90)
    forward(x2)
    left(90)
    forward(y2/2)
    penup()
    goto(x3,y3)
    pendown()
    dot(3,'red')
    done()
    if ( abs(x3+x1)-abs(x1) <abs(x2)-abs(x1) ) and ( abs(y3+y1)-abs(y1) <abs(y2)-abs(y1) ):
    print('point('+str(x3)+','+str(y3)+') is in therectangle ')
    elif ( abs(x3+x1)-abs(x1) == abs(x2)-abs(x1) ) and ( abs(y3+y1)-abs(y1) <abs(y2)-abs(y1) ):
    print('point('+str(x3)+','+str(y3)+') is on thewidth side ')
    elif ( abs(x3+x1)-abs(x1) <abs(x2)-abs(x1) ) and ( abs(y3+y1)-abs(y1) == abs(y2)-abs(y1) ):
    print('point('+str(x3)+','+str(y3)+') is on the lengthside ')
    elif ( abs(x3+x1)-abs(x1) >abs(x2)-abs(x1) ) and ( abs(y3+y1)-abs(y1) >abs(y2)-abs(y1) ):
    print('point('+str(x3)+','+str(y3)+') is out of therectangle ')
    4.23:
    import random
    number=random.randint(1,13)
    if number==1:
       number='A'
    if number==11:
       number='J'
    if number==12:
       number='Q'
    if number==13:
       number='K'
    color=random.randint(1,4)
    if color==1:
       color='梅花'
    if color==2:
       color='红桃'
    if color==3:
       color='方块'
    if color==4:
       color='黑桃'
    print('the card you picked is '+color+str(number))
    4.25:
    x1,y1,x2,y2,x3,y3,x4,y4=eval(input('输入两条线段的四个点坐标:'))
    if (( y1-y2 ) * ( x3-x4 ) - ( x1-x2 ) * ( y3-y4 ) ) != 0:
       x=( ( ( ( y1-y2 ) * x1 - ( x1-x2 )*y1 ) * ( x2-x4 ) )-( ( ( y3-y4 ) *x3-( x3-x4 ) * y3 ) * ( x1-x2 ) ) )/\
           ( ( y1-y2 ) * ( x3-x4 ) - (x1-x2 ) * ( y3-y4 ) )
       y=( ( ( ( y3-y4 ) * x3 - ( x3-x4 )*y3 ) * ( y1-y2 ) )-( ( ( y1-y2 ) *x1-( x1-x2 ) * y1 ) * ( y3-y4 ) ) )/\
           ( ( y1-y2 ) * ( x3-x4 ) - ( x1-x2 ) * ( y3-y4 ) )
    print(x,y)
    else:
    print('eorro')
    4.26:
    number=eval(input('输入一个三位整数:'))
    if99< number <1000 :
    iftype(number) == type(1):        
           a=number // 100
           b=number % 10
    if a==b :
    print(str(number)+'is apalindrome')
    else:
    print(str(number)+'is not apalindrome')
    else:
    print('number is not int')
    else:
    print('input eorro')
    4.27:
    x,y=eval(input('输入一个点的坐标:'))
    if  (0< x <200) and (y < (100-0.5*x)) :
    print('the point is in the triangle')
    else:
    print('the point is not in the triangle')
    4.28:
    x1,y1,w1,h1=eval(input('输入第一个矩形的坐标以及宽和高:'))
    x2,y2,w2,h2=eval(input('输入第二个矩形的坐标以及宽和高:'))
    if (x1-(w1-w2)/2)<=x2<=(x1+(w1-w2)/2) and (y1-(h1-h2)/2)<=y2<=(y1+(h1-h2)/2):
    print('r2 is insider1')
    elif ((x1-(w1+w2)/2)<=x2<(x1-(w1-w2)/2) or (x1+(w1-w2)/2)<x2<=(x1+(w1+w2)/2)) and \
    ((y1-(h1+h2)/2)<=y2<(y1-(h1-h2)/2) or (y1+(h1-h2)/2)<y2<=(y1+(h1+h2)/2)):
    print('r2 overlapsr1')
    else:
    print('r2 does notoverlap r1')
    4.29:
    import math
    x1,y1,r1=eval(input('输入第一个圆的坐标以及半径:'))
    x2,y2,r2=eval(input('输入第二个圆的坐标以及半径:'))
    ifmath.sqrt((x1-x2)**2+(y1-y2)**2) <= abs(r1-r2):
    print('circle2 isinside circle1')
    elifabs(r1-r2)<math.sqrt((x1-x2)**2+(y1-y2)**2)<=r1+r2:
    print('circle2overlaps circle1')
    else:
    print('circle2 doesnot overlaps circle1')
    4.30:
    import time
    zone=eval(input('输入时区:'))
    seconds=int(time.time())
    second=seconds% 60
    miniute=seconds// 60 % 60
    hour=seconds //3600 % 24
    if1<=hour<=12:
    am_pm='am'
    hour+=zone
    print('the currenttime is '+str(hour)+':'+str(miniute)+':'+str(second)+am_pm)
    else:
    am_pm='pm'
    hour=(hour+zone)% 12
    print('the currenttime is '+str(hour)+':'+str(miniute)+':'+str(second)+am_pm)
    4.31:
    x0,y0,x1,y1=eval(input('输入起始点p0和终点p1坐标'))
    x2,y2=eval(input('输入要判断的点p2的坐标'))
    if (x1-x0)*(y2-y0)-(x2-x0)*(y1-y0)>0:
    print('p2在线的左边')
    elif (x1-x0)*(y2-y0)-(x2-x0)*(y1-y0)==0:
    print('p2在线的右边')
    else:
    print('p2在同一条线上')
    4.32:
    x0,y0,x1,y1=eval(input('输入起始点p0和终点p1坐标'))
    x2,y2=eval(input('输入要判断的点p2的坐标'))
    if ((x1-x0)*(y2-y0)-(x2-x0)*(y1-y0))<0:
    print('p2在线上')
    else:
    print('p2不在线上')
    4.33:
    number=eval(input('输入一个十进制整数:'))
    number_ox=number % 16
    if number <= 15:
    if number_ox==10:
           number_ox='A'
    elif number_ox==11:
           number_ox='B'
    elif number_ox==12:
           number_ox='C'
    elif number_ox==13:
           number_ox='D'
    elif number_ox==14:
           number_ox='E'
    elif number_ox==15:
           number_ox='F'
    print(number_ox)
    else:
    print('invalid input')
    4.34:
    ox=input('输入一个十六进制字符:')
    if ox == 'a' or ox=='A':
       print(10)
    elif ox== 'b' or ox=='B':
       print(11)
    elif ox== 'c' or ox=='C':
       print(12)
    elif ox== 'd' or ox=='D':
       print(13)
    elif ox== 'e' or ox=='E':
       print(14)
    elif ox== 'f' or ox=='F':
       print(15)
    elif 0 <= eval(ox) <= 9:
       print(eval(ox))
    else:
       print('eorro')
    4.35-4.39略,与之前题目相似,只是多了绘图过程

    〖下载地址失效反馈〗:

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

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

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

    〖客服24小时咨询〗:

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

    回复

    使用道具 举报

  • TA的每日心情
    擦汗
    2019-12-24 15:55
  • 签到天数: 3 天

    [LV.2]偶尔看看I

    0

    主题

    103

    帖子

    341

    积分

    终身VIP

    Rank: 12Rank: 12Rank: 12

    积分
    341
    发表于 2023-3-24 16:12:06 | 显示全部楼层
    珍爱生命,果断回帖。
    回复

    使用道具 举报

  • TA的每日心情
    开心
    2019-10-28 08:45
  • 签到天数: 6 天

    [LV.2]偶尔看看I

    0

    主题

    107

    帖子

    351

    积分

    终身VIP

    Rank: 12Rank: 12Rank: 12

    积分
    351
    发表于 2023-3-25 05:30:28 | 显示全部楼层
    呵呵。。。
    回复

    使用道具 举报

  • TA的每日心情
    擦汗
    2020-1-22 13:50
  • 签到天数: 15 天

    [LV.4]偶尔看看III

    0

    主题

    107

    帖子

    404

    积分

    终身VIP

    Rank: 12Rank: 12Rank: 12

    积分
    404
    发表于 2023-3-25 13:44:26 | 显示全部楼层
    LZ是天才,坚定完毕
    回复

    使用道具 举报

  • TA的每日心情

    2019-11-23 20:56
  • 签到天数: 8 天

    [LV.3]偶尔看看II

    0

    主题

    103

    帖子

    347

    积分

    终身VIP

    Rank: 12Rank: 12Rank: 12

    积分
    347
    发表于 2023-3-27 05:20:10 | 显示全部楼层
    前排顶,很好!
    回复

    使用道具 举报

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

    [LV.4]偶尔看看III

    0

    主题

    135

    帖子

    485

    积分

    筑基程序员

    Rank: 3Rank: 3

    积分
    485
    发表于 2023-3-27 08:51:01 | 显示全部楼层
    边撸边过
    回复

    使用道具 举报

  • TA的每日心情
    开心
    2021-2-28 11:31
  • 签到天数: 42 天

    [LV.5]常住居民I

    0

    主题

    224

    帖子

    905

    积分

    终身VIP

    Rank: 12Rank: 12Rank: 12

    积分
    905
    发表于 2023-3-27 19:02:27 | 显示全部楼层
    我也顶起出售广告位
    回复

    使用道具 举报

  • TA的每日心情

    2019-11-6 17:11
  • 签到天数: 2 天

    [LV.1]初来乍到

    0

    主题

    91

    帖子

    294

    积分

    终身VIP

    Rank: 12Rank: 12Rank: 12

    积分
    294
    发表于 2023-3-27 19:47:53 | 显示全部楼层
    看起来好像不错的样子
    回复

    使用道具 举报

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

    本版积分规则

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

    QQ群(仅限付费用户)

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