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中使用正则表达式的方法
Aug 13 Python
windows10系统中安装python3.x+scrapy教程
Nov 08 Python
python爬虫实战之最简单的网页爬虫教程
Aug 13 Python
Python爬虫获取图片并下载保存至本地的实例
Jun 01 Python
浅谈python实现Google翻译PDF,解决换行的问题
Nov 28 Python
python将控制台输出保存至文件的方法
Jan 07 Python
Python选择网卡发包及接收数据包
Apr 04 Python
Python程序打包工具py2exe和PyInstaller详解
Jun 28 Python
Django 创建后台,配置sqlite3教程
Nov 18 Python
Python pickle模块实现对象序列化
Nov 22 Python
Pytorch训练过程出现nan的解决方式
Jan 02 Python
Python利用PyPDF2库获取PDF文件总页码实例
Apr 03 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多线程编程之管道通信实例分析
2015/03/07 PHP
详解PHP的Yii框架中组件行为的属性注入和方法注入
2016/03/18 PHP
PHP多进程之pcntl_fork的实例详解
2017/10/15 PHP
javascript 中__proto__和prototype详解
2014/11/25 Javascript
javascript中使用new与不使用实例化对象的区别
2015/06/22 Javascript
JavaScript 数据类型详解
2017/03/13 Javascript
AngulaJS路由 ui-router 传参实例
2017/04/28 Javascript
JS switch判断 三目运算 while 及 属性操作代码
2017/09/03 Javascript
详解Vue的组件中data选项为什么必须是函数
2020/08/17 Javascript
JS性能优化实现方法及优点进行
2020/08/30 Javascript
Vue3配置axios跨域实现过程解析
2020/11/25 Vue.js
简单的Python抓taobao图片爬虫
2014/10/26 Python
Python操作MongoDB数据库PyMongo库使用方法
2015/04/27 Python
Python聚类算法之DBSACN实例分析
2015/11/20 Python
浅谈python中scipy.misc.logsumexp函数的运用场景
2016/06/23 Python
Python实现的微信公众号群发图片与文本消息功能实例详解
2017/06/30 Python
Python实现字符串的逆序 C++字符串逆序算法
2020/05/28 Python
Django添加KindEditor富文本编辑器的使用
2018/10/24 Python
在Python中pandas.DataFrame重置索引名称的实例
2018/11/06 Python
Django数据库连接丢失问题的解决方法
2018/12/29 Python
介绍一款python类型检查工具pyright(推荐)
2019/07/03 Python
Python字符串大小写转换拼接删除空白
2019/09/19 Python
python爬虫 线程池创建并获取文件代码实例
2019/09/28 Python
Python模拟登入的N种方式(建议收藏)
2020/05/31 Python
基于K.image_data_format() == 'channels_first' 的理解
2020/06/29 Python
python3实现语音转文字(语音识别)和文字转语音(语音合成)
2020/10/14 Python
python代码实现猜拳小游戏
2020/11/30 Python
canvas实现滑动验证的实现示例
2020/08/11 HTML / CSS
学院书画协会部门岗位职责
2013/12/01 职场文书
竞争性谈判邀请书
2014/02/06 职场文书
党员实事承诺书
2014/03/26 职场文书
市场部业务员岗位职责
2014/04/02 职场文书
竞选部长演讲稿
2014/04/26 职场文书
创业计划书之面包店
2019/09/12 职场文书
一行代码python实现文件共享服务器
2021/04/22 Python
利用Python实时获取steam特惠游戏数据
2022/06/25 Python