Python趣味挑戰(zhàn)之給幼兒園弟弟生成1000道算術(shù)題
一、前言
阿姨花了30元給幼兒園的小弟弟買了一本習(xí)題,里面都是簡單的二元加減法。我一聽,驚道:“怎么還花錢買題?我動動手指能給你生成一千條。”
阿姨覺得二元加減太簡單了,想要三元加減法的算術(shù)題(x + y + z; x + y - z; x - y - z; x - y + z),因?yàn)榈艿苓€小,只會100以內(nèi)的加減法,不會負(fù)數(shù),所以出的算術(shù)題不僅計算結(jié)果要在[0, 100]內(nèi),算式中的任何兩位的計算也要在[0, 100]內(nèi)。
希望弟弟長大后會感謝我,嘻嘻~
二、思路
生成在[1,99]內(nèi)的隨機(jī)數(shù)x, y, z,若它們的計算結(jié)果在[0, 100]內(nèi),且算式中的任何兩位的計算也在[0, 100]內(nèi),就保存在字符串里,作為答案,如"10 + 13 + 9 = 32";將字符串存入set中,因?yàn)镻ython的set是無序且不重復(fù)的,所以它會自動打亂和去重;把答案寫入文件,寫入文件時要寫入index(題號)去掉結(jié)果再寫入另一個文件,作為題目
三、方法
1.生成隨機(jī)整數(shù):
import random x = random.randint(1, 99) # 生成[1, 99]內(nèi)的整數(shù)
2.set:
s = set() # 初始化要用set() x = 1 s.add(x) # 將x插入s
3.將結(jié)果存入文件
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")
五、結(jié)果
生成的txt文件:


排版后的word文檔:


到此這篇關(guān)于Python趣味挑戰(zhàn)之給幼兒園弟弟生成1000道算術(shù)題的文章就介紹到這了,更多相關(guān)Python生成算術(shù)題內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python實(shí)現(xiàn)音樂下載的統(tǒng)計
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)音樂下載的統(tǒng)計,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-06-06
Python如何將給定字符串中的大寫英文字母按以下對應(yīng)規(guī)則替換
這篇文章主要介紹了Python如何將給定字符串中的大寫英文字母按以下對應(yīng)規(guī)則替換,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-10-10
linux centos 7.x 安裝 python3.x 替換 python2.x的過程解析

