溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Python如何生成算術(shù)題

發(fā)布時(shí)間:2021-05-28 11:47:58 來源:億速云 閱讀:450 作者:小新 欄目:開發(fā)技術(shù)

這篇文章主要介紹了Python如何生成算術(shù)題,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

一、前言

阿姨覺得二元加減太簡(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)。

二、思路

生成在[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文件:

Python如何生成算術(shù)題Python如何生成算術(shù)題

排版后的word文檔:

Python如何生成算術(shù)題
Python如何生成算術(shù)題

感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“Python如何生成算術(shù)題”這篇文章對(duì)大家有幫助,同時(shí)也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,更多相關(guān)知識(shí)等著你來學(xué)習(xí)!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI