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 tempfile模块学习笔记(临时文件)
May 25 Python
Python3编程实现获取阿里云ECS实例及监控的方法
Aug 18 Python
深入理解Python爬虫代理池服务
Feb 28 Python
python3+PyQt5实现拖放功能
Apr 24 Python
Python使用re模块正则提取字符串中括号内的内容示例
Jun 01 Python
使用Python机器学习降低静态日志噪声
Sep 29 Python
python递归法解决棋盘分割问题
Jul 17 Python
对Python函数设计规范详解
Jul 19 Python
Python笔试面试题小结
Sep 07 Python
在Sublime Editor中配置Python环境的详细教程
May 03 Python
Python NumPy灰度图像的压缩原理讲解
Aug 04 Python
python编程简单几行代码实现视频转换Gif示例
Oct 05 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使用strstr()函数获取指定字符串后所有字符的方法
2016/01/07 PHP
深入浅析Yii admin的权限控制
2016/08/31 PHP
PHP基于PDO调用sqlserver存储过程通用方法【基于Yii框架】
2017/10/07 PHP
PHP实现UTF8二进制及明文字符串的转化功能示例
2017/11/20 PHP
在浏览器窗口上添加遮罩层的方法
2012/11/12 Javascript
分析Node.js connect ECONNREFUSED错误
2013/04/09 Javascript
JS文本框追加多个下拉框的值的简单实例
2013/07/12 Javascript
jQuery的控件及事件(输入控件及回车事件)使用示例
2013/07/25 Javascript
js中获取键盘事件的简单实现方法
2016/10/10 Javascript
完美实现js选项卡切换效果(二)
2017/03/08 Javascript
angular.js实现购物车功能
2017/10/23 Javascript
Bootstrap popover 实现鼠标移入移除显示隐藏功能方法
2018/01/24 Javascript
详解vue使用$http服务端收不到参数
2019/04/19 Javascript
vue新建项目并配置标准路由过程解析
2019/12/09 Javascript
mpvue网易云短信接口实现小程序短信登录的示例代码
2020/04/03 Javascript
python基础教程之实现石头剪刀布游戏示例
2014/02/11 Python
python批量同步web服务器代码核心程序
2014/09/01 Python
Python 一句话生成字母表的方法
2019/01/02 Python
对python调用RPC接口的实例详解
2019/01/03 Python
Python比较配置文件的方法实例详解
2019/06/06 Python
pycharm 安装JPype的教程
2019/08/08 Python
Django+uni-app实现数据通信中的请求跨域的示例代码
2019/10/12 Python
Python 爬虫实现增加播客访问量的方法实现
2019/10/31 Python
keras tensorflow 实现在python下多进程运行
2020/02/06 Python
美国一家全面的在线零售鞋类公司:SHOEBACCA
2017/01/06 全球购物
意大利巧克力店:Chocolate Shop
2019/07/24 全球购物
JDBC操作数据库的基本流程是什么
2014/10/28 面试题
竞选团支书演讲稿
2014/04/28 职场文书
和谐家庭演讲稿
2014/05/24 职场文书
用人单位终止解除劳动合同证明书
2014/10/06 职场文书
担保书范本
2015/01/20 职场文书
我的中国梦主题教育活动总结
2015/05/07 职场文书
新员工入职感想
2015/08/07 职场文书
公安干警正风肃纪心得体会
2016/01/15 职场文书
《神奇的鸟岛》教学反思
2016/02/22 职场文书
浅谈Mysql多表连接查询的执行细节
2021/04/24 MySQL