一个简单的自动生成小学数学题的python程序

前言:假期中,许多小学老师要求每天要给学生出一定数量的题目,供学生练习,然后给学生检查,十分麻烦。

import random


# 用来计算最终结果
def calculation_formula(a,b,c,c1,c2):
    if c1 == '+':
        num = a+b
    else:
        num = a-b
    if c2 == '+':
        num = num+c
    else :
        num = num-c
    return num


def random_formula():
    a = random.randint(0, 100)
    b = random.randint(0, 100)
    c = random.randint(0, 100)
    op = ['+', '-']
    c1 = random.choice(op)
    c2 = random.choice(op)
    # 生成式子
    formula = str(a) + c1 + str(b) + c2 + str(c)
    re = calculation_formula(a,b,c,c1,c2)
    string = formula+'='+str(re)
    # 防止出现负数
    if re < 0:
        random_formula()
    else:
        # print(formula,'=',re)
        # 将生成的算式写到文件中去
        with open(r'C:\Users\lch20\Desktop\math\01.txt','a',encoding='utf-8') as f:
            f.write(string)
            # 为了美观
            f.write('\n')
        pass


# 生成25个100以内的加减混合运算
for i in range(25):
    random_formula()
    pass

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值