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清除字符串里非字母字符的方法
Jul 02 Python
浅谈Python数据类型判断及列表脚本操作
Nov 04 Python
Python使用内置json模块解析json格式数据的方法
Jul 20 Python
python中logging库的使用总结
Oct 18 Python
Python数据可视化编程通过Matplotlib创建散点图代码示例
Dec 09 Python
java中两个byte数组实现合并的示例
May 09 Python
Python实现快速计算词频功能示例
Jun 25 Python
python绘制直线的方法
Jun 30 Python
对python使用telnet实现弱密码登录的方法详解
Jan 26 Python
python从内存地址上加载python对象过程详解
Jan 08 Python
Django用数据库表反向生成models类知识点详解
Mar 25 Python
python执行js代码的方法
May 13 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
PHP 表单提交给自己
2008/07/24 PHP
将数组写入txt文件 var_export
2009/04/21 PHP
nginx+php-fpm配置文件的组织结构介绍
2012/11/07 PHP
PHP+redis实现的悲观锁机制示例
2018/06/12 PHP
(currentStyle)javascript为何有时用style得不到已设定的CSS的属性
2007/08/15 Javascript
JavaScript中yield实用简洁实现方式
2010/06/12 Javascript
Angularjs注入拦截器实现Loading效果
2015/12/28 Javascript
浅谈js数据类型判断与数组判断
2016/08/29 Javascript
webpack+vue.js快速入门教程
2016/10/12 Javascript
trackingjs+websocket+百度人脸识别API实现人脸签到
2018/11/26 Javascript
微信小程序 wxParse插件显示视频问题
2019/09/27 Javascript
Js on及addEventListener原理用法区别解析
2020/07/11 Javascript
基于 Vue 的 Electron 项目搭建过程图文详解
2020/07/22 Javascript
vue-cli3中配置alias和打包加hash值操作
2020/09/04 Javascript
一行JavaScript代码如何实现瀑布流布局
2020/12/11 Javascript
[55:02]2014 DOTA2国际邀请赛中国区预选赛 HGT VS Orenda
2014/05/21 DOTA
[02:17]2016完美“圣”典风云人物:Sccc专访
2016/12/03 DOTA
[01:00]一分钟回顾2018DOTA2亚洲邀请赛现场活动
2018/04/07 DOTA
Python使用PyGreSQL操作PostgreSQL数据库教程
2014/07/30 Python
用Python中的字典来处理索引统计的方法
2015/05/05 Python
Python模块搜索概念介绍及模块安装方法介绍
2015/06/03 Python
利用Python实现命令行版的火车票查看器
2016/08/05 Python
tensorflow实现简单逻辑回归
2018/09/07 Python
python批量识别图片指定区域文字内容
2019/04/30 Python
Django静态资源部署404问题解决方案
2020/05/11 Python
在Django中自定义filter并在template中的使用详解
2020/05/19 Python
Keras实现将两个模型连接到一起
2020/05/23 Python
提供世界各地便宜的机票:Sky-tours
2016/07/21 全球购物
法国创作个性化T恤衫和其他定制产品平台:Tostadora
2018/04/08 全球购物
计算机应用专业推荐信
2013/11/13 职场文书
乡镇干部党的群众路线教育实践活动个人对照检查材料
2014/09/24 职场文书
群众路线对照检查剖析材料
2014/10/09 职场文书
银行职员工作失误检讨书
2014/10/14 职场文书
商场圣诞节活动总结
2015/05/06 职场文书
一个成功的互联网创业项目,必须满足这些要求
2019/08/23 职场文书
Nginx本地目录映射实现代码实例
2021/03/31 Servers