Python如何生成算术题

发布时间:2021-05-28 11:47:58 作者:小新
来源:亿速云 阅读:447

这篇文章主要介绍了Python如何生成算术题,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。

一、前言

阿姨觉得二元加减太简单了,想要三元加减法的算术题(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(题号)去掉结果再写入另一个文件,作为题目

三、方法

1.生成随机整数:

import random
x = random.randint(1, 99)	# 生成[1, 99]内的整数

2.set:

s = set()	# 初始化要用set()
x = 1
s.add(x)	# 将x插入s

3.将结果存入文件

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")

五、结果

生成的txt文件:

Python如何生成算术题Python如何生成算术题

排版后的word文档:

Python如何生成算术题
Python如何生成算术题

感谢你能够认真阅读完这篇文章,希望小编分享的“Python如何生成算术题”这篇文章对大家有帮助,同时也希望大家多多支持亿速云,关注亿速云行业资讯频道,更多相关知识等着你来学习!

推荐阅读:
  1. Python生成器
  2. Python随机生成密码

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

python

上一篇:PHP核心特性命名空间的示例分析

下一篇:Java如何实现LRU缓存淘汰算法

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》