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


Posted in Python onMay 28, 2021

一、前言

阿姨花了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文件:

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

排版后的word文档:

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

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

Python 相关文章推荐
在Python中使用zlib模块进行数据压缩的教程
Jun 26 Python
Python编程实现二叉树及七种遍历方法详解
Jun 02 Python
Python实现Smtplib发送带有各种附件的邮件实例
Jun 05 Python
关于Django外键赋值问题详解
Aug 13 Python
python装饰器简介---这一篇也许就够了(推荐)
Apr 01 Python
python3 json数据格式的转换(dumps/loads的使用、dict to str/str to dict、json字符串/字典的相互转换)
Apr 01 Python
详解Django模版中加载静态文件配置方法
Jul 21 Python
详解Python并发编程之创建多线程的几种方法
Aug 23 Python
Python环境Pillow( PIL )图像处理工具使用解析
Sep 12 Python
Windows平台Python编程必会模块之pywin32介绍
Oct 01 Python
详解python内置常用高阶函数(列出了5个常用的)
Feb 21 Python
使用OpenCV校准鱼眼镜头的方法
Nov 26 Python
解决Python中的modf()函数取小数部分不准确问题
May 28 #Python
利用Python+OpenCV三步去除水印
python实现自定义日志的具体方法
May 28 #Python
python 爬取京东指定商品评论并进行情感分析
python b站视频下载的五种版本
May 27 #Python
教你怎么用python selenium实现自动化测试
Python Django框架介绍之模板标签及模板的继承
May 27 #Python
You might like
哪吒敖丙传:新人物二哥敖乙出场 小敖丙奶气十足
2020/03/08 国漫
PHP setcookie指定domain参数后,在IE下设置cookie失效的解决方法
2011/09/09 PHP
PHP开发框架kohana中处理ajax请求的例子
2014/07/14 PHP
php中常量DIRECTORY_SEPARATOR用法深入分析
2014/11/14 PHP
php实现在服务器上创建目录的方法
2015/03/16 PHP
用脚本调用样式的几种方法
2006/12/09 Javascript
用jquery实现等比例缩放图片效果插件
2010/07/24 Javascript
通过javascript获取iframe里的值示例代码
2013/06/24 Javascript
浅析document.createDocumentFragment()与js效率
2013/07/08 Javascript
将json当数据库一样操作的javascript lib
2013/10/28 Javascript
JQuery中使用Ajax赋值给全局变量失败异常的解决方法
2014/08/18 Javascript
Jquery实现简单的轮播效果(代码管用)
2016/03/14 Javascript
javascript 四十条常用技巧大全
2016/09/09 Javascript
轻松理解JavaScript闭包
2017/03/14 Javascript
jquery.form.js异步提交表单详解
2017/04/25 jQuery
ReactNative实现Toast的示例
2017/12/31 Javascript
Nodejs下使用gm圆形裁剪并合成图片的示例
2018/02/22 NodeJs
JS常用的几种数组遍历方式以及性能分析对比实例详解
2018/04/11 Javascript
通过实例解析js简易模块加载器
2019/06/17 Javascript
Phantomjs抓取渲染JS后的网页(Python代码)
2016/05/13 Python
Django 根据数据模型models创建数据表的实例
2018/05/27 Python
Python面向对象之类的定义与继承用法示例
2019/01/14 Python
Django如何开发简单的查询接口详解
2019/05/17 Python
三步实现Django Paginator分页的方法
2019/06/11 Python
Django Rest framework权限的详细用法
2019/07/25 Python
keras-siamese用自己的数据集实现详解
2020/06/10 Python
Python 整行读取文本方法并去掉readlines换行\n操作
2020/09/03 Python
python利用线程实现多任务
2020/09/18 Python
Python爬虫之Selenium警告框(弹窗)处理
2020/12/04 Python
Myprotein丹麦官网:欧洲第一运动营养品牌
2019/04/15 全球购物
护理专业个人求职简历的自我评价
2013/10/13 职场文书
三八活动策划方案
2014/08/17 职场文书
2016大学自主招生推荐信范文
2015/03/23 职场文书
员工福利申请报告
2015/05/15 职场文书
2015年党支部书记工作总结
2015/05/21 职场文书
干部考核工作总结2015
2015/07/24 职场文书