72-数学游戏进阶

与前面例子《71-函数练习:数学游戏》相同,只是加减法函数更换为匿名函数。

from random import randint, choice

def exam():
    cmds = {'+': lambda x, y: x + y, '-': lambda x, y: x - y}
    nums = [randint(1, 100) for i in range(2)]
    nums.sort(reverse=True)
    op = choice('+-')
    result = cmds[op](*nums)
    prompt = "%s %s %s = " % (nums[0], op, nums[1])
    tries = 0

    while tries < 3:
        try:
            answer = int(input(prompt))
        except:
            continue

        if answer == result:
            print('Very good!')
            break
        else:
            print('Wrong answer.')
            tries += 1
    else:
        print('%s%s' % (prompt, result))

if __name__ == '__main__':
    while True:
        exam()
        try:
            yn = input("Continue(y/n)? ").strip()[0]
        except IndexError:
            continue
        except (KeyboardInterrupt, EOFError):
            print()
            yn = 'n'

        if yn in 'nN':
            break
    原文作者:凯茜的老爸
    原文地址: https://www.jianshu.com/p/e0bfb2363b26
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞