用Python给弟弟生成1000道算术题

这篇博客介绍了一个Python脚本,用于自动生成适合幼儿园小朋友练习的三元加减法题目。脚本确保计算结果和中间运算都在[0,100]范围内,并能生成一定数量的独特题目,最后将题目和答案分别写入两个不同的文件中,方便孩子学习使用。
摘要由CSDN通过智能技术生成
  • 动机:阿姨花了30元给幼儿园的小弟弟买了一本习题,里面都是简单的二元加减法。我一听,惊道:“怎么还花钱买题?我动动手指能给你生成一千条。”

    阿姨觉得二元加减太简单了,想要三元加减法的算术题(x + y + z; x + y - z; x - y - z; x - y + z),因为弟弟还小,只会100以内的加减法,不会负数,所以出的算术题不仅计算结果要在[0, 100]内,算式中的任何两位的计算也要在[0, 100]

    希望弟弟长大后会感谢我,嘻嘻~

  • 思路

    1. 生成在[1,99]内的随机数x, y, z,若它们的计算结果在[0, 100]内,且算式中的任何两位的计算也在[0, 100],就保存在字符串里,作为答案,如"10 + 13 + 9 = 32"
    2. 将字符串存入set中,因为Python的set是无序且不重复的,所以它会自动打乱和去重
    3. 答案写入文件,写入文件时要写入index(题号)
    4. 去掉结果再写入另一个文件,作为题目
  • 用到的方法

    1. 生成随机整数:

      import random
      x = random.randint(1, 99)	# 生成[1, 99]内的整数
      
    2. set:

      s = set()	# 初始化要用set()
      x = 1
      s.add(x)	# 将x插入s
      
    3. 将结果存入文件

      text = "Hello world!"
      with open(file, 'a') as f:	# 追加文本到文件
      	# 每次输入前清空文件
      	f.seek(0)
          f.truncate()
      	# 将文本写入文件
          f.write(text)
      
  • 代码

    import random
    
    def fun1(x, y, z):
        s = str(x) + " + " + str(y) + " + " + str(z) + " = " + str(x + y + z)
        return s
    
    def fun2(x, y, z):
        s = str(x) + " + " + str(y) + " - " + str(z) + " = " + str(x + y - z)
        return s
    
    def fun3(x, y, z):
        s = str(x) + " - " + str(y) + " + " + str(z) + " = " + str(x - y + z)
        return s
    
    def fun4(x, y, z):
        s = str(x) + " - " + str(y) + " - " + str(z) + " = " + str(x - y - z)
        return s
    
    def generate(num):
        s = set()
        while len(s) < num:
            x = random.randint(1, 99)
            y = random.randint(1, 99)
            z = random.randint(1, 99)
            if ((x + y >= 0 and x + y <= 100)
                    and (y + z >= 0 and y + z <= 100)
                    and (x + z >= 0 and x + z <= 100)
                    and (x + y + z >= 0 and x + y + z <= 100)):
                s.add(fun1(x, y, z))
            if ((x + y >= 0 and x + y <= 100)
                    and (y - z >= 0 and y - z <= 100)
                    and (x - z >= 0 and x - z <= 100)
                    and (x + y - z >= 0 and x + y - z <= 100)):
                s.add(fun2(x, y, z))
            if ((x - y >= 0 and x - y <= 100)
                    and (- y + z >= 0 and - y + z <= 100)
                    and (x + z >= 0 and x + z <= 100)
                    and (x - y + z >= 0 and x - y + z <= 100)):
                s.add(fun3(x, y, z))
            if ((x - y >= 0 and x - y <= 100)
                    and (- y - z >= 0 and - y - z <= 100)
                    and (x - z >= 0 and x - z <= 100)
                    and (x - y - z >= 0 and x - y - z <= 100)):
                s.add(fun4(x, y, z))
        return s
    
    def save_in_file(answers, answer_file, question_file):
        with open(answer_file, 'a') as f:
            # 每次输入前清空文件
            f.seek(0)
            f.truncate()
    
            cnt = 1
            for ans in answers:
                text = str(cnt) + ")  " + ans + '\n'
                f.write(text)
                cnt += 1
    
        with open(question_file, 'a') as f:
            f.seek(0)
            f.truncate()
    
            cnt = 1
            for ans in answers:
                ques = str(cnt) + ")  " + ans[: ans.find('=') + 1] + "\n"
                f.write(ques)
                cnt += 1
    
    
    save_in_file(generate(1000), 
    "C:\\Users\\sibyl\\Desktop\\calculation\\answer.txt", 
    "C:\\Users\\sibyl\\Desktop\\calculation\\question.txt")
    
  • 结果

    • 生成的txt文件:
      答案题目
    • 排版后的word文档:
      答案
      题目
  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值