Python趣味挑战之给幼儿园弟弟生成1000道算术题

作者:SSibyl 时间:2021-08-13 07:18:56 

一、前言

阿姨花了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道算术题

来源:https://blog.csdn.net/SSibyl/article/details/117287154

标签:Python,生成,算术题
0
投稿

猜你喜欢

  • 实例演示在SQL中启用全文检索

    2011-10-01 14:01:37
  • 在Django的模型中执行原始SQL查询的方法

    2022-12-21 17:24:40
  • Python实现直播弹幕自动发送功能

    2021-08-23 12:31:37
  • python3.x+pyqt5实现主窗口状态栏里(嵌入)显示进度条功能

    2021-12-29 06:55:13
  • python日志通过不同的等级打印不同的颜色(示例代码)

    2022-08-18 01:47:25
  • PHP中PDO事务处理操作示例

    2023-11-21 14:54:12
  • 兼容 IE,Firefox 的图片自动缩放 CSS

    2011-09-27 13:36:58
  • 分析用Python脚本关闭文件操作的机制

    2021-01-25 07:03:26
  • python梯度下降算法的实现

    2022-01-25 11:11:09
  • Python程序设计入门(5)类的使用简介

    2023-01-21 06:27:56
  • python获取当前运行函数名称的方法实例代码

    2023-03-06 15:34:22
  • python pandas处理excel表格数据的常用方法总结

    2023-06-02 19:49:47
  • python 包之 multiprocessing 多进程

    2021-02-25 12:13:15
  • Flask  request 对象介绍

    2022-11-01 23:45:35
  • python linecache 处理固定格式文本数据的方法

    2022-10-23 21:06:05
  • python实现自动化办公邮件合并功能

    2022-02-22 21:06:06
  • python中的queue队列类型及函数用法

    2023-09-04 21:04:01
  • pandas时间序列之pd.to_datetime()的实现

    2022-09-21 23:47:29
  • 详解Python中映射类型(字典)操作符的概念和使用

    2021-12-05 09:24:43
  • python实现彩色图转换成灰度图

    2022-03-11 02:41:30
  • asp之家 网络编程 m.aspxhome.com