用Python给弟弟生成1000道算术题
动机:阿姨花了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(题号)
- 去掉结果再写入另一个文件,作为题目
- 生成在
用到的方法:
生成随机整数:
import random x = random.randint(1, 99) # 生成[1, 99]内的整数
set:
s = set() # 初始化要用set() x = 1 s.add(x) # 将x插入s
将结果存入文件
text = "Hello world!" with open(file, 'a') as f: # 追加文本到文件# 每次输入前清空文件f.seek(0)f.truncate()# 将文本写入文件f.write(text)
代码:
import randomdef fun1(x, y, z):s = str(x) + " + " + str(y) + " + " + str(z) + " = " + str(x + y + z)return sdef fun2(x, y, z):s = str(x) + " + " + str(y) + " - " + str(z) + " = " + str(x + y - z)return sdef fun3(x, y, z):s = str(x) + " - " + str(y) + " + " + str(z) + " = " + str(x - y + z)return sdef fun4(x, y, z):s = str(x) + " - " + str(y) + " - " + str(z) + " = " + str(x - y - z)return sdef 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 sdef save_in_file(answers, answer_file, question_file):with open(answer_file, 'a') as f:# 每次输入前清空文件f.seek(0)f.truncate()cnt = 1for ans in answers:text = str(cnt) + ") " + ans + '\n'f.write(text)cnt += 1with open(question_file, 'a') as f:f.seek(0)f.truncate()cnt = 1for ans in answers:ques = str(cnt) + ") " + ans[: ans.find('=') + 1] + "\n"f.write(ques)cnt += 1save_in_file(generate(1000), "C:\\Users\\sibyl\\Desktop\\calculation\\answer.txt", "C:\\Users\\sibyl\\Desktop\\calculation\\question.txt")
结果:
- 生成的txt文件:
- 排版后的word文档:
- 生成的txt文件:
用Python给弟弟生成1000道算术题相关推荐
- Python练习题答案: 馏分类【难度:2级】--景越Python编程实例训练营,1000道上机题等你来挑战
馏分类[难度:2级]: 答案1: # Something goes Here ...class Fraction:def __init__(self, numerator, denominator): ...
- Python练习题答案: IRR计算 - 盈利能力的评价【难度:2级】--景越Python编程实例训练营,1000道上机题等你来挑战
IRR计算 - 盈利能力的评价[难度:2级]: 答案1: def irr(c):precision, guess, v, lastPositiveGuess, lastNegativeGuess = ...
- Python练习题答案: 杰克的家【难度:2级】--景越Python编程实例训练营,1000道上机题等你来挑战
杰克的家[难度:2级]: 答案1: VERSES = """\ This is the house that Jack built.This is the malt th ...
- Python练习题答案: 分类新会员【难度:1级】--景越Python编程实例训练营,1000道上机题等你来挑战
分类新会员[难度:1级]: 答案1: def openOrSenior(data):return ["Senior" if age >= 55 and handicap &g ...
- Python练习题答案: CIS 122#12中的构造【难度:1级】--景越Python编程实例训练营,1000道上机题等你来挑战
CIS 122#12中的构造[难度:1级]: 答案1: # For your convenience, the following functions from previous challenges ...
- Python练习题答案: 转换货币II【难度:2级】--景越Python编程实例训练营,1000道上机题等你来挑战
转换货币II[难度:2级]: 答案1: def solution(to,lst):dolSym, eurSym, power = ('', '€', -1) if to=='EUR' else ('$ ...
- Python练习题答案: 海盗!是大炮准备好了!?【难度:0级】--景越Python编程实例训练营,1000道上机题等你来挑战
海盗!是大炮准备好了!?[难度:0级]: 答案1: def cannons_ready(gunners):return 'Shiver me timbers!' if 'nay' in gunners ...
- Python练习题答案: 摩门经【难度:2级】--景越Python编程实例训练营,1000道上机题等你来挑战
摩门经[难度:2级]: 答案1: from math import log, ceil def mormons(starting_number, reach, target):return ceil( ...
- Python练习题答案: 财富通灵塔的乘驾【难度:3级】--景越Python编程实例训练营,1000道上机题等你来挑战
财富通灵塔的乘驾[难度:3级]: 答案1: def ride_of_fortune(artifact, explorers):STATES = {'A': 'B', 'B': 'A'}DIRS = { ...
- Python练习题答案: 赛车#1:简化拖动赛【难度:1级】--景越Python编程实例训练营,1000道上机题等你来挑战
赛车#1:简化拖动赛[难度:1级]: 答案1: def drag_race(length, anna, bob):a = length / anna.speed + anna.reaction_tim ...
最新文章
- jquery iCheck 插件
- [Go] golang设置运行的cpu数
- [十一]SpringBoot 之 添加JSP支持
- Android之解决CoordinatorLayout+AppBarLayout+CollapsingToolbarLayout+RecyclerView里面再嵌套RecyclerView滑动颤抖问题
- OSChina 周四乱弹 ——妹子喜欢的是程序员 这是标准……
- mysql进阶-01-视图
- LINUX 文件合并,去重
- Value of key '' is not a string! Cannot translate the value of keypath ''. Use the v
- 各地级市系列环境指标数据(2003-2017年)
- Java8 map转list集合
- 248 中心对称数 III
- 一张图告诉你三大运营商2G/3G/4G频率分配和网络制式
- iPhone开发Swift基础08 加密与安全
- eNSP综合实验——简易园区网的搭建
- 我的修炼体会--明亭【转】
- AT2565 Chocolate Bar 洛谷
- 爬虫入门经典(四) | 如何爬取豆瓣电影Top250
- 液晶电视英文linux使用教程,液晶电视如何使用 液晶电视正确使用方法介绍【详解】...
- Springboot中cache的使用
- Html实现滚动字幕效果
热门文章
- 解决双启动GRLDR missing故障的方法
- 申论(基础题)之应用文写作
- [后端开发]支付宝支付接口调试 (Python v3.6)
- WindowsServer2012R2 SSL/TLS 受诫礼(BAR-MITZVAH)攻击漏洞(CVE-2015-2808)解决办法
- 老瞎眼 pk 小鲜肉-线段树+区间离线
- [python][turtle]闪瞎眼的晶体管报时
- 网络游戏开发实战-坦克大战学习问题记录
- HTML_龙湖地产界面自制
- php 时间格式大全
- java throw异常_Java throw Exception实现异常转换