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 相关文章推荐
PyQt5每天必学之滑块控件QSlider
Apr 20 Python
python向已存在的excel中新增表,不覆盖原数据的实例
May 02 Python
Linux下多个Python版本安装教程
Aug 15 Python
numpy 对矩阵中Nan的处理:采用平均值的方法
Oct 30 Python
对python 多线程中的守护线程与join的用法详解
Feb 18 Python
PyCharm 配置远程python解释器和在本地修改服务器代码
Jul 23 Python
Flask框架学习笔记之使用Flask实现表单开发详解
Aug 12 Python
python 在threading中如何处理主进程和子线程的关系
Apr 25 Python
python3中编码获取网页的实例方法
Nov 16 Python
Python .py生成.pyd文件并打包.exe 的注意事项说明
Mar 04 Python
利用Python判断整数是否是回文数的3种方法总结
Jul 07 Python
Python的property属性详细讲解
Apr 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
php链表用法实例分析
2015/07/09 PHP
Jquery实现页面加载时弹出对话框代码
2013/04/19 Javascript
js中数组Array的一些常用方法总结
2013/08/12 Javascript
JS连连看源码完美注释版(推荐)
2013/12/09 Javascript
JavaScript中的分号插入机制详细介绍
2015/02/11 Javascript
举例讲解如何判断JavaScript中对象的类型
2016/04/22 Javascript
JavaScript使用正则表达式获取全部分组内容的方法示例
2017/01/17 Javascript
教你快速搭建Node.Js服务器的方法教程
2017/03/30 Javascript
js 获取今天以及过去日期
2017/04/11 Javascript
jQuery鼠标悬停内容动画切换效果
2017/04/27 jQuery
nodejs微信扫码支付功能实现
2018/02/17 NodeJs
vue项目打包部署_nginx代理访问方法详解
2018/09/20 Javascript
jQuery each和js forEach用法比较
2019/02/27 jQuery
详解JavaScript中的函数、对象
2019/04/01 Javascript
微信小程序清空输入框信息与实现屏幕往上滚动的示例代码
2020/06/23 Javascript
vue v-for出来的列表,点击某个li使得当前被点击的li字体变红操作
2020/07/17 Javascript
js实现电灯开关效果
2021/01/19 Javascript
[40:03]DOTA2上海特级锦标赛主赛事日 - 1 败者组第一轮#1EHOME VS Archon
2016/03/02 DOTA
浅析Python中的getattr(),setattr(),delattr(),hasattr()
2016/06/14 Python
python 读取摄像头数据并保存的实例
2018/08/03 Python
pygame游戏之旅 创建游戏窗口界面
2018/11/20 Python
Python判断telnet通不通的实例
2019/01/26 Python
Python流行ORM框架sqlalchemy安装与使用教程
2019/06/04 Python
Django实现跨域的2种方法
2019/07/31 Python
python递归调用中的坑:打印有值, 返回却None
2020/03/16 Python
Python基于stuck实现scoket文件传输
2020/04/02 Python
CSS3 text shadow字体阴影效果
2016/01/08 HTML / CSS
html5-Canvas可以在web中绘制各种图形
2012/12/26 HTML / CSS
h5网页水印SDK的实现代码示例
2019/02/19 HTML / CSS
伦敦所有西区剧院演出官方票务代理:Theatre Tickets Direct
2017/05/26 全球购物
德国购买健身器材:AsVIVA
2017/08/09 全球购物
参赛口号
2014/06/16 职场文书
课内比教学心得体会
2014/09/09 职场文书
公司合作协议范文
2014/10/01 职场文书
团员个人年度总结
2015/02/26 职场文书
使用vuex-persistedstate本地存储vuex
2022/04/29 Vue.js