zoukankan      html  css  js  c++  java
  • 《Python核心编程》第二版第308页第十一章练习 续四 Python核心编程答案自己做的

    本博客列出的答案不是来自官方资源,是我自己做的练习,如果有疑问或者错误,欢迎讨论。

    11-16.
    更新easyMath.py。这个脚本,如例子11.1描绘的那样,以入门程序来帮助年轻人强化他们的数学技能。通过加入乘法作为可支持的操作来更进一步提升这个程序。额外的加分:也加入除法;这比较难做因为你要找到有效的整形除数,幸运的是,已经有代码来确定分子比分母大,所以不需要支持分数。
    【答案】
    添加了乘法后,代码如下:

    #-*- encoding: utf-8 -*-
    # easyMath.py
    
    from operator import add, sub, mul
    from random import randint, choice
    
    ops = {'+': add, '-': sub, '*': mul}
    
    MAXTRIES = 2
    
    def doprob():
        op = choice('+-*')
        nums = [randint(1, 10) for i in range(2)]
        nums.sort(reverse = True)
        ans = ops[op](*nums)
        pr = '%d %s %d = ' % (nums[0], op, nums[1])
        oops = 0
        while True:
            try:
                if int(raw_input(pr)) == ans:
                    print 'Correct!'
                    break
                if oops == MAXTRIES:
                    print 'Answer\n%s%d' % (pr, ans)
                else:
                    print 'Incurrect... try again'
                    oops += 1
            except (KeyboardInterrupt, EOFError, ValueError):
                print 'Invalid input... try again'
                
    def main():
        while True:
            doprob()
            try:
                opt = raw_input('Again? [y]').lower()
                if opt and opt[0] == 'n':
                    break
            except (KeyboardInterrupt, EOFError):
                break
        
    if __name__ == '__main__':
        main()
                


    添加了除法后,代码如下:

    #-*- encoding: utf-8 -*-
    # easyMath.py
    
    from operator import add, sub, mul, div
    from random import randint, choice
    
    ops = {'+': add, '-': sub, '*': mul, '/': div}
    # From www.cnblogs.com/balian/
    
    MAXTRIES = 2
    
    def doprob():
        op = choice('+-*/')
        nums = [randint(1, 10) for i in range(2)]
        nums.sort(reverse = True)
        if op != '/':
            ans = ops[op](*nums)
            pr = '%d %s %d = ' % (nums[0], op, nums[1])
        else:
            ans = div(nums[0], nums[1])
            if div(nums[0] * 10, nums[1]) == ans * 10: # 这里用来判断是否能整除
                pr = '%d %s %d = ' % (nums[0], op, nums[1])
            else:
                ans = mul(nums[0], nums[1]) # 如果不能整除的话,将运算符修改成乘法
                pr = '%d %s %d = ' % (nums[0], '*', nums[1])
        oops = 0
        while True:
            try:
                if int(raw_input(pr)) == ans:
                    print 'Correct!'
                    break
                if oops == MAXTRIES:
                    print 'Answer\n%s%d' % (pr, ans)
                else:
                    print 'Incurrect... try again'
                    oops += 1
            except (KeyboardInterrupt, EOFError, ValueError):
                print 'Invalid input... try again'
                
    def main():
        while True:
            doprob()
            try:
                opt = raw_input('Again? [y]').lower()
                if opt and opt[0] == 'n':
                    break
            except (KeyboardInterrupt, EOFError):
                break
    
    # From www.cnblogs.com/balian/    
    if __name__ == '__main__':
        main()
     

    11-17.定义
    (a)描述偏函数应用和currying之间的区别。
    (b)偏函数应用和闭包之间有什么区别?
    (c)最后,迭代器和生成器是怎么区别开的?
    【未完】
    感觉本题要说清楚有点难度,暂时押后。

  • 相关阅读:
    3星|《全球电商进化史》:全球电商亲历记
    2星|陈春花《共生》:逻辑差语文差缺证据。不敢相信知名商学院教授的书居然这么差
    3星|《第五次开始》:考古学家写的人类简史与未来简史
    4星|《财经》2018年第21期:互联网处方能解决药品质量和价格问题
    2.5星|托夫勒《权力的转移》;30年旧书,现在看理论有点牵强肤浅,预测有的准有的不准
    2018左其盛好书榜(截至9月15日)
    沟通交流技巧相关的11本书点评
    没睡好觉的上级更容易辱骂下属:3.5星|《哈佛商业评论》第9期
    3星|《利润模式》:20年旧书,30种模式
    在 C# 中通过 P/Invoke 调用Win32 DLL
  • 原文地址:https://www.cnblogs.com/balian/p/2626045.html
Copyright © 2011-2022 走看看