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的chardet库获得文件编码并修改编码
Jan 22 Python
在Mac OS上使用mod_wsgi连接Python与Apache服务器
Dec 24 Python
深入解析Python小白学习【操作列表】
Mar 23 Python
python实现弹跳小球
May 13 Python
Python实现个人微信号自动监控告警的示例
Jul 03 Python
django多文件上传,form提交,多对多外键保存的实例
Aug 06 Python
python3中替换python2中cmp函数的实现
Aug 20 Python
Python 实现将数组/矩阵转换成Image类
Jan 09 Python
python 工具 字符串转numpy浮点数组的实现
Mar 14 Python
利用scikitlearn画ROC曲线实例
Jul 02 Python
python 使用xlsxwriter循环向excel中插入数据和图片的操作
Jan 01 Python
Matplotlib可视化之添加让统计图变得简单易懂的注释
Jun 11 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
模拟OICQ的实现思路和核心程序(三)
2006/10/09 PHP
PHP中mb_convert_encoding与iconv函数的深入解析
2013/06/21 PHP
浅析PHP中的字符串编码转换(自动识别原编码)
2013/07/02 PHP
PHP实现小偷程序实例
2016/10/31 PHP
php5.3后静态绑定用法详解
2016/11/11 PHP
javascript与webservice的通信实现代码
2010/12/25 Javascript
Jquery给基本控件的取值、赋值示例
2014/05/23 Javascript
使用mini-define实现前端代码的模块化管理
2014/12/25 Javascript
jQuery实现折线图的方法
2015/02/28 Javascript
JS实现跟随鼠标立体翻转图片的方法
2015/05/04 Javascript
Bootstrap每天必学之级联下拉菜单
2016/03/27 Javascript
实现隔行换色效果的两种方式【实用】
2016/11/27 Javascript
js时间戳格式化成日期格式的多种方法介绍
2017/02/16 Javascript
js学使用setTimeout实现轮循动画
2017/07/17 Javascript
javascript如何用递归写一个简单的树形结构示例
2017/09/06 Javascript
微信小程序仿微信运动步数排行(交互)
2018/07/13 Javascript
js数组去重的方法总结
2019/01/18 Javascript
30分钟用Node.js构建一个API服务器的步骤详解
2019/05/24 Javascript
微信公众号获取用户地理位置并列出附近的门店的示例代码
2019/07/25 Javascript
JS XMLHttpRequest原理与使用方法深入详解
2020/04/30 Javascript
python 快速排序代码
2009/11/23 Python
python解析xml模块封装代码
2014/02/07 Python
用Python解决计数原理问题的方法
2016/08/04 Python
python根据文章标题内容自动生成摘要的实例
2019/02/21 Python
python ChainMap 合并字典的实现步骤
2019/06/11 Python
python3实现高效的端口扫描
2019/08/31 Python
CSS3 linear-gradient线性渐变生成加号和减号的方法
2017/11/21 HTML / CSS
南非领先的在线旅行社:Travelstart南非
2016/09/04 全球购物
Perfume’s Club澳大利亚官网:西班牙领先的在线美容店
2021/02/01 全球购物
工作睡觉检讨书
2014/02/25 职场文书
商务助理求职信范文
2014/04/20 职场文书
小学清明节活动总结
2014/07/04 职场文书
简单通用的简历自我评价
2014/09/21 职场文书
2015年端午节国旗下演讲稿
2015/03/19 职场文书
Nginx优化服务之网页压缩的实现方法
2021/03/31 Servers
Python使用UDP实现720p视频传输的操作
2021/04/24 Python