Python趣味挑战之给幼儿园弟弟生成1000道算术题

认真发呆
认真发呆
发布时间:2026-10-14 | 2人浏览过
原创

本文介绍如何使用Python为幼儿园儿童生成1000道三元加减法算术题。通过随机生成1-99的整数,利用集合set的特性实现自动去重和打乱,并严格限制运算结果及中间步骤均在0-100范围内,确保题目适合低龄儿童。

一、前言

阿姨花费30元为幼儿园的小弟弟购买了一本习题册,内容仅包含简单的二元加减法。得知此事后,我表示无需花钱购买,只需简单操作即可生成一千道题目。

阿姨认为二元加减法过于简单,希望增加三元加减法的题目(x + y + z; x + y - z; x - y - z; x - y + z)。考虑到弟弟年龄尚小,仅掌握100以内的加减法且未接触负数概念,因此生成的算术题需满足两个条件:计算结果必须落在[0, 100]范围内,且算式中任意两个数之间的运算结果也必须保持在[0, 100]范围内。

希望弟弟长大后能感谢我的这份用心,哈哈~

二、思路

首先生成位于[1,99]范围内的随机数x, y, z。若这些数的计算结果在[0, 100]内,且算式中任意两位数的运算结果也在[0, 100]内,则将其保存为字符串形式的答案,例如"10 + 13 + 9 = 32"。随后,将这些字符串存入set中。由于Python中的set数据结构具有无序且不重复的特性,它能自动对答案进行打乱并去除重复项。最后,将答案写入文件,并在写入题目文件时,通过提取index(题号)并去除结果部分,生成对应的题目文件。

三、方法

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文档:


到此这篇关于Python趣味挑战之给幼儿园弟弟生成1000道算术题的文章就介绍到这了,更多相关Python生成算术题内容请搜索本站以前的文章或继续浏览下面的相关文章希望大家以后多多支持本站!

相关标签:

本站声明

本文内容仅供参考,不代表本站观点。转载或引用请注明出处。如发现内容问题,请通过联系我们页面反馈。

Python 相关产品和应用

全部下载

专题列表