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实现的百度站长自动URL提交小工具
Jun 27 Python
python实现逆波兰计算表达式实例详解
May 06 Python
使用Python导出Excel图表以及导出为图片的方法
Nov 07 Python
Python实现多进程共享数据的方法分析
Dec 04 Python
pandas系列之DataFrame 行列数据筛选实例
Apr 12 Python
python数字图像处理实现直方图与均衡化
May 04 Python
Python 类,property属性(简化属性的操作),@property,property()用法示例
Oct 12 Python
python中p-value的实现方式
Dec 16 Python
Python使用qrcode二维码库生成二维码方法详解
Feb 17 Python
opencv+python实现均值滤波
Feb 19 Python
python pandas.DataFrame.loc函数使用详解
Mar 26 Python
Python字符串对齐方法使用(ljust()、rjust()和center())
Apr 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
在PHP中读取和写入WORD文档的代码
2008/04/09 PHP
在命令行下运行PHP脚本[带参数]的方法
2010/01/22 PHP
PHP获取redis里不存在的6位随机数应用示例【设置24小时过时】
2017/06/07 PHP
PHP封装的完整分页类示例
2018/08/21 PHP
php成功操作redis cluster集群的实例教程
2019/01/13 PHP
PHP开发的文字水印,缩略图,图片水印实现类与用法示例
2019/04/12 PHP
PHP实现基本留言板功能原理与步骤详解
2020/03/26 PHP
jQuery 添加/移除CSS类实现代码
2010/02/11 Javascript
jquery关于图形报表的运用实现代码
2011/01/06 Javascript
js获取select默认选中的Option并不是当前选中值
2014/05/07 Javascript
ECMAScript 6即将带给我们新的数组操作方法前瞻
2015/01/06 Javascript
网页瀑布流布局jQuery实现代码
2016/10/21 Javascript
javascript设置文本框光标的方法实例小结
2016/11/04 Javascript
vue-loader教程介绍
2017/06/14 Javascript
在vue中使用echarts(折线图的demo,markline用法)
2020/07/20 Javascript
python中字典(Dictionary)用法实例详解
2015/05/30 Python
python使用Image处理图片常用技巧分析
2015/06/01 Python
python选择排序算法实例总结
2015/07/01 Python
Python聚类算法之基本K均值实例详解
2015/11/20 Python
numpy中的ndarray方法和属性详解
2019/05/27 Python
Pytorch在dataloader类中设置shuffle的随机数种子方式
2020/01/14 Python
用HTML5实现鼠标滚轮事件放大缩小图片的功能
2015/06/25 HTML / CSS
德国著名廉价网上药店:Shop-Apotheke
2017/07/23 全球购物
Bose法国官网:购买耳机、扬声器、家庭影院、专业音响
2017/12/21 全球购物
马歇尔耳机官网:Marshall Headphones
2020/02/04 全球购物
简历中的自我评价怎么写
2014/01/29 职场文书
银行职员自我鉴定
2014/04/20 职场文书
小学生感恩演讲稿
2014/04/25 职场文书
安全资料员岗位职责范本
2014/06/28 职场文书
2014年秋季开学寄语
2014/08/02 职场文书
ktv周年庆活动方案
2014/08/18 职场文书
学校联谊协议书
2014/09/16 职场文书
计算机实训报告范文
2014/11/05 职场文书
四年级小学生评语
2014/12/26 职场文书
高质量“欢迎词”
2019/04/03 职场文书
Python基础之教你怎么在M1系统上使用pandas
2021/05/08 Python