zoukankan      html  css  js  c++  java
  • Python编写两个数的加减法游戏

    目标:

      1.实现两个数的加减法

      2.回答者3次输错计算结果后,输出正确结果,并询问回答者是否继续

    1.使用常规函数实现两个数的加减法游戏

    代码如下:

    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    
    '''使用常规函数编写出题游戏'''
    
    import random
    
    def add(x,y):
        return x + y
    
    def sub(x,y):
        return x - y
    
    def chuti():
        cmds = {'+': add, '-': sub}
        ops = '+-'
        op = random.choice(ops)
        nums = [random.randint(1,50) for i in xrange(2)]
        nums.sort(reverse=True)
        prompt = '%s %s %s = ' %(nums[0], op, nums[1])
        anwser = cmds[op](*nums)
    
        counter = 0 
        while counter < 3:
            try:
                result = int(raw_input(prompt))
            except:
                continue
            if anwser == result:
                print "回答正确"
                print "-" * 20 
                break
            else:
                counter += 1
                print "回答错误"
                print "-" * 20
        else:
            print "正确答案是: %s %s" % (prompt, anwser)
    
    
    if __name__ == "__main__":
        while True:
            chuti()
            try:
                yn = raw_input("Continue(y/n?)").strip()[0]
            except IndexError:
                continue
            except (KeyboardInterrupt,EOFError):
                yn = 'n'
            if yn in 'Nn':
                print "结束"
                break

    •运行代码,测试效果

    [root@localhost python]# python new_mathgame.py
    27 + 25 = 5
    回答错误
    --------------------
    27 + 25 = 2
    回答错误
    --------------------
    27 + 25 = 3
    回答错误
    --------------------
    正确答案是: 27 + 25 =  52
    Continue(y/n?)y
    15 - 1 = 12
    回答错误
    --------------------
    15 - 1 = 13
    回答错误
    --------------------
    15 - 1 = 14
    回答正确
    --------------------
    Continue(y/n?)n
    结束

    2.使用lambda匿名函数实现两位数的加减法游戏

    代码如下:

    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    
    '''使用匿名函数lambda编写出题游戏'''
    
    import random
    
    # def add(x,y):
    #    return x + y
    
    # def sub(x,y):
    #    return x - y
    def chuti():
        cmds = {'+': lambda x, y: x + y, '-': lambda x, y: x - y}
        ops = '+-'
        op = random.choice(ops)
        nums = [random.randint(1,50) for i in xrange(2)]
        nums.sort(reverse=True)
        prompt = '%s %s %s = ' %(nums[0], op, nums[1])
        anwser = cmds[op](*nums)
    
        counter = 0 
        while counter < 3:
            try:
                result = int(raw_input(prompt))
            except:
                continue
            if anwser == result:
                print "回答正确"
                print "-" * 20 
                break
            else:
                counter += 1
                print "回答错误"
                print "-" * 20
        else:
            print "正确答案是: %s %s" % (prompt, anwser)
    
    
    if __name__ == "__main__":
        while True:
            chuti()
            try:
                yn = raw_input("Continue(y/n?)").strip()[0]
            except IndexError:
                continue
            except (KeyboardInterrupt,EOFError):
                yn = 'n'
            if yn in 'Nn':
                print "结束"
                break
  • 相关阅读:
    C/C++取出变量的每一位的值(第一次知道还有QBitArray)
    什么样的程序员适合去创业公司
    VC2008如何生成及使用DLL(图文并茂,完整版)
    Qt浅谈之二十六图片滑动效果
    Qt 学习之路 2(75):线程总结
    Big Data Ingestion and streaming product introduction
    Qt学习之路(24): QPainter(改写paintEvent)
    Qt学习之路(54): 自定义拖放数据对象
    Qt学习之路(49): 通用算法
    Qt核心剖析: moc
  • 原文地址:https://www.cnblogs.com/xkops/p/6264323.html
Copyright © 2011-2022 走看看