最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間:2021年05月28日 08:56:35   作者:SSibyl  
為了讓弟弟以后好好學(xué)習(xí),我特地用Python給他生成了1000道算術(shù)題讓他做,他以后一定會(huì)感謝我的!文中有非常詳細(xì)的代碼示例,需要的朋友可以參考下

一、前言

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

阿姨覺得二元加減太簡單了,想要三元加減法的算術(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)。

希望弟弟長大后會(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)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Python使用paddleOCR批量識(shí)別pdf的方法

    Python使用paddleOCR批量識(shí)別pdf的方法

    PaddleOCR可以在圖像、文本、表格等多種場(chǎng)景下進(jìn)行文字識(shí)別,本文主要介紹了Python使用paddleOCR批量識(shí)別pdf的方法,具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-03-03
  • Linux下為不同版本python安裝第三方庫

    Linux下為不同版本python安裝第三方庫

    本文給大家分享了下作者是如何在linux下為python2.x以及python3.x安裝第三方庫的方法,十分的實(shí)用,有需要的小伙伴可以參考下
    2016-08-08
  • linux centos 7.x 安裝 python3.x 替換 python2.x的過程解析

    linux centos 7.x 安裝 python3.x 替換 python2.x的過程解析

    這篇文章主要介紹了linux centos 7.x 安裝 python3.x 替換 python2.x的過程解析,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-12-12
  • Python地圖繪制實(shí)操詳解

    Python地圖繪制實(shí)操詳解

    在本文里我們給大家介紹了用Python繪制地圖的知識(shí)點(diǎn)以及詳細(xì)步驟,需要的朋友們跟著學(xué)習(xí)下。
    2019-03-03
  • Python編寫打字訓(xùn)練小程序

    Python編寫打字訓(xùn)練小程序

    這篇文章主要介紹了Python編寫打字訓(xùn)練小程序,需要的朋友可以參考下
    2019-09-09
  • 淺談Python 中的復(fù)數(shù)問題

    淺談Python 中的復(fù)數(shù)問題

    這篇文章主要介紹了在Python 中的復(fù)數(shù)問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2021-05-05
  • Web自動(dòng)化之Selenium常用操作方法大全

    Web自動(dòng)化之Selenium常用操作方法大全

    Selenium是一種自動(dòng)化測(cè)試工具,可以用于測(cè)試Web應(yīng)用程序,它提供了一組用于自動(dòng)化Web瀏覽器進(jìn)行測(cè)試的API,下面這篇文章主要給大家介紹了關(guān)于Web自動(dòng)化之Selenium常用操作方法的相關(guān)資料,需要的朋友可以參考下
    2023-06-06
  • 最新評(píng)論

    苍梧县| 德州市| 丰城市| 元谋县| 青海省| 射洪县| 循化| 宜州市| 石柱| 永定县| 通海县| 柳河县| 启东市| 扶绥县| 利川市| 定州市| 邵武市| 襄垣县| 丹凤县| 康乐县| 田林县| 巫溪县| 恩施市| 沽源县| 元谋县| 永康市| 清涧县| 虎林市| 华亭县| 巴楚县| 平凉市| 周宁县| 陇川县| 安庆市| 墨竹工卡县| 东方市| 广东省| 广丰县| 集贤县| 离岛区| 兴义市|