Python core programming version 2, 308th page, Chapter 4 exercises continued 4-answers to Python core programming-self-developed-

Source: Internet
Author: User
Tags mul

The answer in this blog is not from official resources, but from my own exercises. If you have any questions or errors, please discuss them.

11-16.
Update easyMath. py. This script, as described in example 11.1, uses an entry program to help young people strengthen their mathematical skills. This program is further upgraded by adding multiplication as a supported operation. Extra bonus points: add Division. This is difficult because you need to find a valid integer divisor. Fortunately, you already have code to determine that the numerator score is greater than the parent, so you do not need to support the score.
[Answer]
After multiplication is added, the Code is as follows:

#-*- encoding: utf-8 -*-# easyMath.pyfrom operator import add, sub, mulfrom random import randint, choiceops = {'+': add, '-': sub, '*': mul}MAXTRIES = 2def 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()            

After division is added, the Code is as follows:

#-*-Encoding: UTF-8-*-# easyMath. pyfrom operator import add, sub, mul, divfrom random import randint, choiceops = {'+': add, '-': sub, '*': mul ,'/': div} # From www.cnblogs.com/balian/MAXTRIES = 2def 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: # Here we will judge whether pr = '% d % s % d =' % (nums [0], op, nums [1]) else can be divisible: ans = mul (nums [0], nums [1]) # If division is not allowed, change the operator to multiplication 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 'currect... try again 'oops + = 1 T (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 handle T (KeyboardInterrupt, EOFError ): break # From www.cnblogs.com/balian/ if _ name _ = '_ main _': main ()
 

 

11-17. Definition
(A) Describe the difference between partial function applications and currying.

(B) What is the difference between partial function applications and closures?

(C) At last, how are the iterators and generators different?

[Unfinished]

It is difficult to clarify this question.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.