ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

2021-2022-1 20211420《信息安全专业导论》小学四则运算编程实践

2021-11-17 01:32:36  阅读:189  来源: 互联网

标签:10 2022 randint random write 20211420 2021 str append


设计思路

小学四则运算少不了自然数,符号,等号等等,所以需要将每一个功能都定义一个函数

代码

# 导入库并定义列表
import random
import fractions

questions = []
answers = []
#实现四则运算基本功能
def randomcalc(questions, answers):
    symbol = random.choice(["+", "-", "×", "÷"])
    if symbol == "+":
        a = random.randint(0,10)
        b = random.randint(0,10)
        while a == b:
            a = random.randint(0, 10)
            b = random.randint(0, 10)
        else:
            questions.append(str(a) + '+' + str(b) + '=')
            answers.append(a + b)
    elif symbol == "-":
        a = random.randint(0,10)
        b = random.randint(0,10)
        while a == b:
            a = random.randint(0, 10)
            b = random.randint(0, 10)
        else:
            questions.append(str(a) + '-' + str(b) + '=')
            answers.append(a - b)
    elif symbol == "×":
        a = random.randint(0,10)
        b = random.randint(0,10)
        while a == b:
            a = random.randint(0, 10)
            b = random.randint(0, 10)
        else:
            questions.append(str(a) + '×' + str(b) + '=')
            answers.append(a * b)
    else:
        a = random.randint(0, 10)
        if a == 0:
            b = random.randint(1, 10)
        else:
            b = random.randint(1, a+1)
        while a == b:
            a = random.randint(0, 10)
            if a == 0:
                b = random.randint(1, 10)
            else:
                b = random.randint(1, a + 1)
        else:
            questions.append(str(a) + '÷' + str(b) + '=')
            answers.append(fractions.Fraction(a, b))
# 生成分数
def createfraction():
    fenzi1 = random.randint(1, 10)
    fenmu1 = random.randint(1, 10)
    fenzi2 = random.randint(1, 10)
    fenmu2 = random.randint(1, 10)
    fraction1 = fractions.Fraction(fenzi1, fenmu1)
    fraction2 = fractions.Fraction(fenzi2, fenmu2)
    return fraction1, fraction2
# 两个分数四则运算(输出应为大于0的真分数)
def fractioncalc(questions, answers):
    symbol = random.choice(["+", "-", "×", "÷"])
    a, b = createfraction()
    if symbol == "+":
        while a+b>1:
            a, b = createfraction()
        while a == b:
            a, b = createfraction()
        else:
            questions.append(str(a) + '+' + str(b) + '=')
            answers.append(a + b)
    elif symbol == "-":
        a, b = createfraction()
        a, b = max(a, b), min(a, b)         # 防止出现负数
        while a == b:
            a, b = createfraction()
        else:
            questions.append(str(a) + '-' + str(b) + '=')
            answers.append(a - b)
    elif symbol == "×":
        while a*b>1:
            a, b = createfraction()
        while a == b:
            a, b = createfraction()
        else:
            questions.append(str(a) + '×' + str(b) + '=')
            answers.append(a * b)
    else:
        while a/b>1:
            if b == 0:
                b += 1
            else:
                a, b = createfraction()
        while a == b:
            if b == 0:
                b += 1
            else:
                a, b = createfraction()
        else:
            questions.append(str(a) + '÷' + str(b) + '=')
            answers.append(fractions.Fraction(a, b))
# 输出函数
def output():
    n = k = 10000  # 第一个while循环的n值会改变
    m = 0
    txt1 = open('exercises.txt', 'w+')
    txt2 = open('answers.txt', 'w')
    # 写入10000道题目
    while n>0:
        choice = random.choice([0, 1])
        if choice == 0:
            randomcalc(questions, answers)
        else:
            fractioncalc(questions, answers)
        n -= 1
    # 向两个文件中分别写入题目和答案
    while m<k:
        question = questions[m]
        answer = answers[m]
        txt1.write(str(m+1) + '.')
        txt2.write(str(m + 1) + '.')
        txt1.write(question)
        txt2.write(str(answer))
        txt1.write('\n')
        txt2.write('\n')
        m += 1
# 实现统计错题,判定对错以及计算得分的功能
def judge():
    k = 10000
    print("请将题目的答案按题号写入answer.txt文件中")
    text = input("请输入\'yes'获得文本:")
    if text == 'yes':
        youranswer = open('answer.txt')
        answers = open('answers.txt')
        x = list(youranswer)
        y = list(answers)
        o = 0
        C = 0
        Correct = []
        Wrong = []
        while o < k:
            if x[o] == y[o]:
                t = o + 1
                Correct.append(t)
            else:
                C += 1
                p = o + 1
                Wrong.append(p)
            o += 1
        Grade = k - C
        G = open('Grade.txt', 'w')
        G.write('Correct:')
        G.write(str(Correct))
        G.write('\n')
        G.write('Wrong:')
        G.write(str(Wrong))
        G.write('\n')
        G.write('Grade:')
        G.write(str(Grade))
        G.close()

# 判断重复题目数量
def repeatjudger():
    repeatfile = open('repeat.txt', 'w')
    repeater = input('请输入continue以继续程序:')
    open('repeat.txt', 'w')
    if repeater == 'continue':
        repeat = []
        repeatnum = 0
        j = 0
        for i in range(0, 10000):
            while j < i:
                if questions[i] == questions[j]:
                    repeatnum += 1
                    repeatq = questions[i]
                    repeat.append(questions[i])
                    repeatfile.write('重复的题目是:')
                    repeatfile.write(repeatq)
                    repeatfile.write('\n')
                    repeatfile.write('对应的题号为:')
                    repeatfile.write(str(j+1))
                    repeatfile.write('和')
                    repeatfile.write(str(i+1))
                    repeatfile.write('\n')
                    j += 1
                else:
                    break

        repeatfile.write('\n')
        repeatfile.write('重复题目的数量为:')
        repeatfile.write(str(repeatnum))
        repeatfile.close()
        print(repeat)
    print("程序结束!")

到这里基本已经实现任务目标,但是还有一些细节并没有处理得当。至于生成的题目数量,我这用的10000道题目,可以更多,也可以较少,只要你的计算机不会崩溃就能够运行。

标签:10,2022,randint,random,write,20211420,2021,str,append
来源: https://www.cnblogs.com/shibatori/p/15565322.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有