python代码随机生成加减乘除混合运算题,给熊孩子练习很不错哦!!!
寒假在家不能让他闲着,熊孩子的计算能力一如既往的差,学渣老爹继续根据学习进度自动生成寒假作业。不多说了,直接上代码,本着能用就好的原则,并没有调优,也没有把对应功能抽成函数。

代码:(没有过滤九九乘法表规则,有点乱)

import random
#定义多个变量并赋值
exp1,exp2 = "",""
str1,str2 = "",""
j=0
count=int(input("请输入出题数量:"))
while j<count:
    if j<count:                              # 随机产生"+"或 "-"号 或"*"号 或"/"号
        flag = random.choice(["+", "-", "*", "/"])   # 随机产生"+"或 "-"号 或"*"号 或"/"号
        if flag == "+":
            a = random.randint(0, 100)
            b = random.randint(0, 100-a)
            result=a+b
        # 如果是减法,被减数和减数都应小于100
        elif flag == "-":
            a = random.randint(1, 100)
            b = random.randint(1, 100)
            # ab比较,较大的数为被减数
            if a < b:
               a,b=b,a
            result=a-b
        elif flag == "*":
            a = random.randint(1, 100)
            b = random.randint(1, 100)
            result=a*b
        elif flag == "/":
            a = random.randint(1, 100)
            b = random.randint(1, 100)
            if a < b:
               a,b=b,a
            result=int(a/b)
        a = str(a).ljust(2, " ")       # ljust左对齐
        b = str(b).ljust(2, " ")
        exp1 = a +" "+ flag +" "+ b + " ="
        exp2 = a +" "+flag +" "+ b + " ="+str(result)
        if j%2==0:                       #  j为偶数,不换行
            str1 = str1 + exp1 + '\t'
            str2 = str2 + exp2 + '\t'        
        else:                              #  j为奇数,换行
            str1 = str1 + exp1 + '\n'
            str2 = str2 + exp2 + '\n'
        j=j+1
with open('math.txt','w') as f:
    f.write(str1)
with open('key.txt','w') as f:
    f.write(str2)
print(count,"道混合加减乘除法题:")    
print(str1)
print(count,"道混合加减乘除法题(带答案):")  
print(str2)

运行截图:

python的加减乘除 python加减乘除混合运算_python

在新浪博客找到了一个比较好的代码,可参考:
注:如果是加号,保证两个数都不超过50,目前学的都是100以内的数;
如果是除号,保证在乘法口诀范围内即可。
代码实现:

import random
string = ""

for day in range(1, 31):
    string = ""
    string = string + "\n快乐寒假第 " + str(day) + " 天:\n(1)快速口算\n"
    #用来记录每行算术题的个数
    num = 0
    #生成每天20道加减乘除
    for i in range(1, 13):
        flag = random.choice([" + ", " - ", " × ", " ÷ "])
        exp = ""
        # 如果是加号,保证两个数都不超过50,目前学的都是100以内的数
        if flag == " + ":
            a = str(random.randint(1, 50)).ljust(2, " ")
            b = str(random.randint(1, 50)).ljust(2, " ")
            exp = a + flag + b
        # 如果是减号,保证被减数和减数都小于等于100即可
        if flag == " - ":
            a = random.randint(1, 100)
            b = random.randint(1, 100)
            # 保证较大的数作为被减数
            if a < b:
                tmp = a
                a = b
                b = tmp
            a = str(a).ljust(2, " ")
            b = str(b).ljust(2, " ")
            exp = a + flag + b
        # 如果是乘号,保证两个数都小于10即可
        if flag == " × ":
            a = str(random.randint(0, 9)).ljust(2, " ")
            b = str(random.randint(0, 9)).ljust(2, " ")
            exp = a + flag + b
        # 如果是除号,保证在乘法口诀范围内即可
        if flag == " ÷ ":
            a = random.choice([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 14, 15, 16, 18, 20, 21, 24, 25, 27, 28, 30, 32, 35, 36, 40,
42, 45, 48, 49, 54, 56, 63, 64, 72, 81])
            if a == 81:
                b = 9
            if a == 72:
                b = random.choice([1, 8, 9])
            if a == 64:
                b = 8
            if a == 63:
                b = random.choice([1, 7, 9])
            if a == 56:
                b = random.choice([1, 7, 8])
            if a == 54:
                b = random.choice([1, 6, 9])
            if a == 49:
               b = 7
            if a == 48:
                b = random.choice([1, 6, 8])
            if a == 45:
                b = random.choice([1, 5, 9])
            if a == 42:
                b = random.choice([1, 6, 7])
            if a == 40:
                b = random.choice([1, 5, 8])
            if a == 36:
                b = random.choice([1, 4, 9])
            if a == 35:
                b = random.choice([1, 5, 7])
            if a == 32:
                b = random.choice([1, 4, 8])
            if a == 30:
                b = random.choice([1, 6, 5])
            if a == 28:
                b = random.choice([1, 4, 7])
            if a == 27:
                b = random.choice([1, 3, 9])
            if a == 25:
                b = random.choice([1, 5])
            if a == 24:
                b = random.choice([1, 3, 4, 6, 8])
            if a == 21:
                b = random.choice([1, 3, 7])
            if a == 20:
                b = random.choice([1, 4, 5])
            if a == 18:
                b = random.choice([1, 2, 3, 6, 9])
            if a == 16:
                b = random.choice([1, 2, 4, 8])
            if a == 15:
                b = random.choice([1, 3, 5])
            if a == 14:
                b = random.choice([1, 2, 7])
            if a == 12:
                b = random.choice([1, 2, 3, 4, 6])
            if a == 10:
                b = random.choice([1, 2, 5])
            if a == 9:
                b = random.choice([1, 3, 9])
            if a == 8:
                b = random.choice([1, 2, 4, 8])
            if a == 7:
                b = random.choice([1, 7])
            if a == 6:
                b = random.choice([1, 2, 3, 6])
            if a == 5:
                b = random.choice([1, 5])
            if a == 4:
                b = random.choice([1, 2, 4])
            if a == 3:
                b = random.choice([1, 3])
            if a == 2:
                b = random.choice([1, 2])
            if a == 1:
                b = 1
            if a == 0:
                b = random.randint(1, 9)
            a = str(a).ljust(2, " ")
            b = str(b).ljust(2, " ")
            exp = a + flag + b
        if num < 3:
            string = string + exp + " =".ljust(7, " ")
            num = num + 1
        # 达到4个题目,换行
        else:
            string = string + exp + "=\n"
            num = 0

    string = string + "(2)提高口算\n"
    ###用来生成两个运算符的算式
    # 用来记录每行算术题的个数
    num = 0
    for i in range(1, 7):
        result = 0
        flag = random.choice([" + ", " - ", " × ", " ÷ "])
        exp = ""
        # 如果是加号,保证两个数都不超过50,目前学的都是100以内的数
        if flag == " + ":
            a = str(random.randint(1, 50)).ljust(2, " ")
            b = str(random.randint(1, 50)).ljust(2, " ")
            exp = a + flag + b
            result = int(a) + int(b)
        # 如果是减号,保证被减数和减数都小于等于100即可
        if flag == " - ":
            a = random.randint(1, 100)
            b = random.randint(1, 100)
            # 保证较大的数作为被减数
            if a < b:
                tmp = a
                a = b
                b = tmp
            a = str(a).ljust(2, " ")
            b = str(b).ljust(2, " ")

            exp = a + flag + b
            result = int(a) - int(b)

        # 如果是乘号,保证两个数都小于10即可
        if flag == " × ":
            a = str(random.randint(0, 9)).ljust(2, " ")
            b = str(random.randint(0, 9)).ljust(2, " ")
            exp = a + flag + b
            result = int(a) * int(b)
        # 如果是除号,保证在乘法口诀范围内即可
        if flag == " ÷ ":
            a = random.choice([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 14, 15, 16, 18, 20, 21, 24, 25, 27, 28, 30, 32, 35, 36, 40,
42, 45, 48, 49, 54, 56, 63, 64, 72, 81])
            if a == 81:
                b = 9
            if a == 72:
                b = random.choice([1, 8, 9])
            if a == 64:
                b = 8
            if a == 63:
                b = random.choice([1, 7, 9])
            if a == 56:
                b = random.choice([1, 7, 8])
            if a == 54:
                b = random.choice([1, 6, 9])
            if a == 49:
                b = 7
            if a == 48:
                b = random.choice([1, 6, 8])
            if a == 45:
                b = random.choice([1, 5, 9])
            if a == 42:
                b = random.choice([1, 6, 7])
            if a == 40:
                b = random.choice([1, 5, 8])
            if a == 36:
                b = random.choice([1, 4, 9])
            if a == 35:
                b = random.choice([1, 5, 7])
            if a == 32:
                b = random.choice([1, 4, 8])
            if a == 30:
                b = random.choice([1, 6, 5])
            if a == 28:
                b = random.choice([1, 4, 7])
            if a == 27:
                b = random.choice([1, 3, 9])
            if a == 25:
                b = random.choice([1, 5])
            if a == 24:
                b = random.choice([1, 3, 4, 6, 8])
            if a == 21:
                b = random.choice([1, 3, 7])
            if a == 20:
                b = random.choice([1, 4, 5])
            if a == 18:
                b = random.choice([1, 2, 3, 6, 9])
            if a == 16:
                b = random.choice([1, 2, 4, 8])
            if a == 15:
                b = random.choice([1, 3, 5])
            if a == 14:
                b = random.choice([1, 2, 7])
            if a == 12:
                b = random.choice([1, 2, 3, 4, 6])
            if a == 10:
                b = random.choice([1, 2, 5])
            if a == 9:
                b = random.choice([1, 3, 9])
            if a == 8:
                b = random.choice([1, 2, 4, 8])
            if a == 7:
                b = random.choice([1, 7])
            if a == 6:
                b = random.choice([1, 2, 3, 6])
            if a == 5:
                b = random.choice([1, 5])
            if a == 4:
                b = random.choice([1, 2, 4])
            if a == 3:
                b = random.choice([1, 3])
            if a == 2:
                b = random.choice([1, 2])
            if a == 1:
                b = 1
            if a == 0:
                b = random.randint(1, 9)
            a = str(a).ljust(2, " ")
            b = str(b).ljust(2, " ")
            exp = a + flag + b
            result = int(a) / int(b)

        flag = random.choice([" + ", " - "])
        # 如果是加号,保证和不超过100,目前学的都是100以内的数
        if flag == " + ":
            a = str(random.randint(1, 100 - result)).ljust(2, " ")
            exp = exp + flag + a
        # 如果是减号,保证能够减得着即可
        if flag == " - ":
            a = str(random.randint(0, result)).ljust(2, " ")
            exp = exp + flag + a
        if num < 2:
            string = string + exp + " =".ljust(7, " ")
            num = num + 1
        # 达到3个题目,换行
        else:
            string = string + exp + "=\n"
            num = 0

    string = string + "(3)竖式计算\n"
    ###用来生成需要运用竖式运算的题目
    # 用来记录每行算术题的个数
    num = 0
    for i in range(1, 4):
        result = 0
        flag = random.choice([" + ", " - "])
        exp = ""
        # 如果是加号,保证两个数都不超过50,目前学的都是100以内的数
        if flag == " + ":
            a = str(random.randint(1, 50)).ljust(2, " ")
            b = str(random.randint(1, 50)).ljust(2, " ")
            exp = a + flag + b
            result = int(a) + int(b)
        # 如果是减号,保证被减数和减数都小于等于100即可
        if flag == " - ":
            a = random.randint(1, 100)
            b = random.randint(1, 100)
        # 保证较大的数作为被减数
            if a < b:
                tmp = a
                a = b
                b = tmp
            a = str(a).ljust(2, " ")
            b = str(b).ljust(2, " ")

            exp = a + flag + b
            result = int(a) - int(b)

        flag = random.choice([" + ", " - "])
        # 如果是加号,保证和不超过100,目前学的都是100以内的数
        if flag == " + ":
            a = str(random.randint(1, 100 - result)).ljust(2, " ")
            exp = exp + flag + a
        # 如果是减号,保证能够减的着即可
        if flag == " - ":
            a = str(random.randint(0, result)).ljust(2, " ")
            exp = exp + flag + a
        if num < 2:
            string = string + exp + " =".ljust(7, " ")
            num = num + 1
        # 达到3个题目,换行
        else:
            string = string + exp + "=\n\n\n\n\n"
            num = 0
    print(string)

运行结果:

python的加减乘除 python加减乘除混合运算_python的加减乘除_02