zoukankan      html  css  js  c++  java
  • 小学四则运算

    import random
    #四则运算

    def t():
        sym = ['+', '-', '×', '÷']
        f= random.randint(0, 3)
        n1 = random.randint(1, 20)
        n2 = random.randint(1, 20)
        result = 0
        if f== 0:#加法
            result  = n1 + n2
        elif f == 1:#减法,要先比较大小,防止输出负数
            n1, n2 = max(n1, n2), min(n1, n2)
            result  = n1 - n2
        elif f== 2:#乘法
            result  = n1 * n2
        elif f == 3:#除法,要比较大小,并循环取整除
            n1, n2 = max(n1, n2), min(n1, n2)
            while n1 % n2 != 0:
                n1 = random.randint(1, 10)
                n2 = random.randint(1, 10)
                n1, n2 = max(n1, n2), min(n1, n2)
            result  = int(n1 / n2)
        print(n1, sym[f], n2, '= ', end='')
        return result
    #制作题库
    def test():

        print('输入所需要的题目数量')
        n=int(input())
        result =[]
        m=0
        while m<=(n-1):
            print(m+1,end='、')
            result .append(t())
            print(' ')
            m=m+1
        m=0
        print('对应的答案:')
        while m<=(n-1):
            print(m+1,'、',result [m])
            m=m+1
    print('选择想要的模式')
    print('1、进行四则运算')
    print('2、制作题库')
    n=int(input())
    #当输入1时,进行四则运算,调用函数t()
    if n==1:
        while True:
            result  = t()
            j= input()
            s= int(j)
            if s== result :
                print('right')
            else:
                print('error.,the answer is', result )
    #当输入921时,进行制作题库
    if n==921:
        test()

    代码运行如下

  • 相关阅读:
    转!idea 破解版 安装
    原!!自动备份 发布脚本
    原!linux脚本统计
    转!!mybatis xml 传值 if test判断
    转!mysql备份与还原数据库
    转!mysql 命令行下 通过DELIMITER临时改变语句分隔符 执行存储过程
    原!统计文件 包含关键字 数量
    原!mysql存储过程 批量导入数据
    转!!Linux 里的 2>&1 究竟是什么
    原!linux 监控 jar定时任务 挂了重启 脚本
  • 原文地址:https://www.cnblogs.com/ssssspm/p/14057493.html
Copyright © 2011-2022 走看看