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

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

    [LV.5]常住居民I

    20

    主题

    66

    帖子

    376

    积分

    筑基程序员

    Rank: 3Rank: 3

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

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

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

    x
    4.13:
    import sys
    status= eval(input('(0-singlefiler,1-married jointly,\n'+
    '2-married separately,3-head of household)\n'+
    'enter the filing status:'))
    income = eval(input('enter thetaxable income:'))
    tax=0
    if status == 0:
    if income <= 8350:
           tax = income * 0.10
    elif income <33950 :
           tax=8350*0.10+(income-8350)*0.15
    elif income <82250 :
           tax=8350*0.10+(33950-8350)*0.15+(income-33950)*0.25
    elif income <171550:
           tax=8350*0.10+(33950-8350)*0.15+(82250-33950)*0.25+\
               (income-82250)*0.28
    elif income <372950 :
           tax=8350*0.10+(33950-8350)*0.15+(82250-33950)*0.25+\
               (171550-82250)*0.28+(income-171550)*0.33
    else:
           tax=8350*0.10+(33950-8350)*0.15+(82250-33950)*0.25+\
               (171550-82250)*0.28+(372950-171550)*0.33+\
                    (income-372950)*0.35
    elif status==1:
    if income <= 16700:
           tax = income * 0.10
    elif income <67900 :
           tax=16700*0.10+(income-16700)*0.15
    elif income <137050 :
           tax=16700*0.10+(67900-16700)*0.15+(income-67900)*0.25
    elif income <208805:
           tax=16700*0.10+(67900-16700)*0.15+(137050-67900)*0.25+\
               (income-137050)*0.28
    elif income <372950 :
           tax=16700*0.10+(67900-16700)*0.15+(137050-67900)*0.25+\
               (208805-137050)*0.28+(income-208805)*0.33
    else:
    16700*0.10+(67900-16700)*0.15+(137050-67900)*0.25+\
               (208805-137050)*0.28+(372950-208805)*0.33+\
                    (income-372950)*0.35
    elif status == 2:
    if income <= 8350:
           tax = income * 0.10
    elif income <33950 :
           tax=8350*0.10+(income-8350)*0.15
    elif income <68525 :
           tax=8350*0.10+(33950-8350)*0.15+(income-33950)*0.25
    elif income <104425:
           tax=8350*0.10+(33950-8350)*0.15+(68525-33950)*0.25+\
                (income-68525)*0.28
    elif income <186475 :
           tax=8350*0.10+(33950-8350)*0.15+(68525-33950)*0.25+\
               (104425-68525)*0.28+(income-104425)*0.33
    else:
    8350*0.10+(33950-8350)*0.15+(68525-33950)*0.25+\
               (104425-68525)*0.28+(186475-104425)*0.33+\
                    (income-186475)*0.35
    elif status == 3:
    if income <= 11950:
           tax = income * 0.10
    elif income <45500 :
           tax=11950*0.10+(income-11950)*0.15
    elif income <117450 :
           tax=11950*0.10+(45500-11950)*0.15+(income-45500)*0.25
    elif income <190200:
           tax=11950*0.10+(45500-11950)*0.15+(117450-45500)*0.25+\
               (income-117450)*0.28
    elif income <372950 :
           tax=11950*0.10+(45500-11950)*0.15+(117450-45500)*0.25+\
               (190200-117450)*0.28+(income-190200)*0.33
    else:
    11950*0.10+(45500-11950)*0.15+(117450-45500)*0.25+\
               (190200-117450)*0.28+(372950-190200)*0.33+\
                    (income-372950)*0.35
    else:
    print('error')
       sys.exit
    print('tax is ',format(tax,'.2f'))
    4.14:
    import random
    coin=random.randint(0,1)
    number=eval(input('正面为1,背面为0,你猜是?'))
    print( coin==number if coin == number else coin==number)
    4.15:
    import random
    guess=eval(input('你猜中奖号码是多少?'))
    number=random.randint(0,999)
    guess1=guess // 100
    guess2=guess // 10 % 10
    guess3=guess % 10
    number1=number // 100
    number2=number // 100 % 10
    number3=number % 10
    number_list=[number1,number2,number3]
    if guess==number:
    print('bingo!you win all 10000$')
    elif (number1==guess1 or number1==guess2 or number1==guess3) and\
        ((number2==guess1 or number2==guess2or number2==guess3) and\
        (number3==guess1 or number3==guess2or number3==guess3)) :
    print('match all number,you win 3000$')
    elif ((guess1 in number_list) and (guess2 notin number_list) and (guess3 notin number_list)) or\
        ((guess2 in number_list) and (guess1 notin number_list) and (guess3 notin number_list)) or\
        ((guess3 in number_list) and (guess1 notin number_list) and (guess2 notin number_list)):
    print('you just match one number,you win 1000$')
    else:
    print('sorry,you guess nnumber is wrong ,no money')
    print(guess,number)
    4.16:
    import random
    number=random.randint(ord('A'),ord('Z'))
    print(chr(number))
    4.17:
    import random
    computer=random.randint(0,2)
    people=eval(input('scissor(0),rock(1),paper(2):'))
    if people==computer:
    print('the computer is '+str(computer)+'youare '+str(people)+'too.'+'it is a draw')
    elif computer==0and people==1:
    print('the computer is '+str(computer)+'youare '+str(people)+'you win !')
    elif computer==0and people==2:
    print('the computer is '+str(computer)+'youare '+str(people)+'the computer win !')
    elif computer==1and people==0:
    print('the computer is '+str(computer)+'youare '+str(people)+'the computer win !')
    elif computer==1and people==2:
    print('the computer is '+str(computer)+'youare '+str(people)+'you win !')
    elif computer==2and people==0:
    print('the computer is '+str(computer)+'youare '+str(people)+'you win !')
    elif computer==2and people==1:
    print('the computer is '+str(computer)+'youare '+str(people)+'the computer win !')
    else:
    print('input eorro')
    4.18:
    rate=eval(input('enter theexchange rate from dollars to RMB:'))
    number=eval(input('enter 0 toconvert dollars to RMB and 1 vice versa:'))
    if number == 0:
       dollars=eval(input('enter the dollars amount:'))
       rmb=dollars*rate
    print(str(dollars)+'dollarsis',format(rmb,'.2f')+'yuan')
    elif number==1:
       rmb=eval(input('enter the rmb amount:'))
       dollars=rmb/rate
    print(str(rmb)+'rmb is',format(dollars,'.2f')+'dollars')
    else:
    print('incorrect input')
    4.19:
    side1,side2,side3=eval(input('输入三角形三条边长:'))
    if side1+side2>side3 and side2+side3>side1 and side1+side3>side2:
       perimeter=side1+side2+side3
    print('the perimeter is '+str(perimeter))
    else:
    print('the input is invalid')

    〖下载地址失效反馈〗:

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

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

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

    〖客服24小时咨询〗:

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

    回复

    使用道具 举报

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

    [LV.2]偶尔看看I

    0

    主题

    93

    帖子

    305

    积分

    终身VIP

    Rank: 12Rank: 12Rank: 12

    积分
    305
    发表于 2023-3-24 01:41:41 | 显示全部楼层
    支持你哈...................................
    回复

    使用道具 举报

  • TA的每日心情

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

    [LV.3]偶尔看看II

    0

    主题

    103

    帖子

    347

    积分

    终身VIP

    Rank: 12Rank: 12Rank: 12

    积分
    347
    发表于 2023-3-25 05:31:11 | 显示全部楼层
    在撸一遍。。。
    回复

    使用道具 举报

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

    [LV.2]偶尔看看I

    0

    主题

    103

    帖子

    341

    积分

    终身VIP

    Rank: 12Rank: 12Rank: 12

    积分
    341
    发表于 2023-3-27 03:35:56 | 显示全部楼层
    楼猪V5啊
    回复

    使用道具 举报

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

    本版积分规则

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

    QQ群(仅限付费用户)

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