濮阳杆衣贸易有限公司

主頁 > 知識(shí)庫 > Python趣味挑戰(zhàn)之給幼兒園弟弟生成1000道算術(shù)題

Python趣味挑戰(zhàn)之給幼兒園弟弟生成1000道算術(shù)題

熱門標(biāo)簽:開封自動(dòng)外呼系統(tǒng)怎么收費(fèi) 開封語音外呼系統(tǒng)代理商 河北防封卡電銷卡 天津電話機(jī)器人公司 地圖標(biāo)注線上如何操作 電銷機(jī)器人的風(fēng)險(xiǎn) 應(yīng)電話機(jī)器人打電話違法嗎 手機(jī)網(wǎng)頁嵌入地圖標(biāo)注位置 400電話辦理哪種

一、前言

阿姨花了30元給幼兒園的小弟弟買了一本習(xí)題,里面都是簡(jiǎn)單的二元加減法。我一聽,驚道:“怎么還花錢買題?我動(dòng)動(dòng)手指能給你生成一千條?!?/p>

阿姨覺得二元加減太簡(jiǎn)單了,想要三元加減法的算術(shù)題(x + y + z; x + y - z; x - y - z; x - y + z),因?yàn)榈艿苓€小,只會(huì)100以內(nèi)的加減法,不會(huì)負(fù)數(shù),所以出的算術(shù)題不僅計(jì)算結(jié)果要在[0, 100]內(nèi),算式中的任何兩位的計(jì)算也要在[0, 100]內(nèi)。

希望弟弟長(zhǎng)大后會(huì)感謝我,嘻嘻~

二、思路

生成在[1,99]內(nèi)的隨機(jī)數(shù)x, y, z,若它們的計(jì)算結(jié)果在[0, 100]內(nèi),且算式中的任何兩位的計(jì)算也在[0, 100]內(nèi),就保存在字符串里,作為答案,如"10 + 13 + 9 = 32";將字符串存入set中,因?yàn)镻ython的set是無序且不重復(fù)的,所以它會(huì)自動(dòng)打亂和去重;把答案寫入文件,寫入文件時(shí)要寫入index(題號(hào))去掉結(jié)果再寫入另一個(gè)文件,作為題目

三、方法

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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

您可能感興趣的文章:
  • 使用python批量生成insert語句的方法
  • python基礎(chǔ)學(xué)習(xí)之生成器與文件系統(tǒng)知識(shí)總結(jié)
  • python生成器generator:深度學(xué)習(xí)讀取batch圖片的操作
  • 教你怎么用Python生成九宮格照片
  • Python如何生成隨機(jī)高斯模糊圖片詳解
  • python使用ProjectQ生成量子算法指令集
  • 教你使用Python根據(jù)模板批量生成docx文檔
  • Python實(shí)現(xiàn)K-means聚類算法并可視化生成動(dòng)圖步驟詳解
  • python基于opencv批量生成驗(yàn)證碼的示例
  • 用python自動(dòng)生成日歷

標(biāo)簽:常州 駐馬店 山東 成都 蘭州 江蘇 宿遷 六盤水

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Python趣味挑戰(zhàn)之給幼兒園弟弟生成1000道算術(shù)題》,本文關(guān)鍵詞  Python,趣味,挑戰(zhàn),之,給,幼兒園,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《Python趣味挑戰(zhàn)之給幼兒園弟弟生成1000道算術(shù)題》相關(guān)的同類信息!
  • 本頁收集關(guān)于Python趣味挑戰(zhàn)之給幼兒園弟弟生成1000道算術(shù)題的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    托里县| SHOW| 驻马店市| 双江| 绥芬河市| 肇庆市| 鹤山市| 虞城县| 高淳县| 读书| 孝昌县| 建阳市| 教育| 漳平市| 丹棱县| 福鼎市| 贵港市| 寻甸| 固镇县| 梁河县| 阜康市| 新干县| 安阳县| 南漳县| 武汉市| 合江县| 仁布县| 墨脱县| 正安县| 曲松县| 江津市| 大英县| 奉新县| 苗栗市| 铅山县| 霸州市| 黄梅县| 梅河口市| 堆龙德庆县| 庆安县| 贵溪市|