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.