小学四则运算编程题

遇到的问题:
1 在GRADE文件导出的题目有问题
2 会出现非整数的题目 不知道怎么解决

from fractions import Fraction
import random

q =[]
ans = []
right = []
wrong = []
right_num = []
wrong_num = []
not_all_repeat = []
repeat1 = []
repeat2 = []
repeat_ans = []

def cal1():
hao = random.choice(['+', '-', '*', '/'])
n1 = random.randint(0,10)
n2 = random.randint(0,10)
n3 = random.randint(1,10)
if hao == '+':
a = str(n1) + '+' + str(n2) + '='
b = n1 + n2
return a, b

elif hao == "-":
    a = str(n1) + '-' + str(n2) + '='
    b = n1 - n2
    return a, b
    
elif hao == "*":
    a = str(n1) + '*' + str(n2) + '='
    b = n1 * n2
    return a, b
  
elif hao == "/":
    a = str(n1) + '/' + str(n3) + '='
    b = Fraction(n1, n3)
    return a, b

def cal2():
hao = random.choice(['+' + '-', '+' + '', '+' + "/", "-" + "", '-' + "/", "*" + "/"])
n1 = random.randint(0,10)
n2 = random.randint(0,10)
n4 = random.randint(0,10)
n3 = random.randint(1,10)
if hao == '+' + '-':
a = str(n1) + '+' + str(n2) + "-" + str(n4) + '='
b = n1 + n2 - n4
return a, b

elif hao == "+" + "*":
    a = str(n1) + '+' + str(n2) + "*" + str(n4) + '='
    b = n1 + n2 * n4
    return a, b
    
elif hao == "+" + "/":
    a = str(n1) + '+' + str(n2) + "/" + str(n3) + '='
    b = n1 + Fraction(n2 , n3)
    return a, b

elif hao == "-" + "*":
    a = str(n1) + '-' + str(n2) + "*" + str(n4) + '='
    b = n1 - n2 * n4
    return a, b

elif hao == "-" + "/":
    a = str(n1) + '-' + str(n2) + "/" + str(n3) + '='
    b = n1 - Fraction(n2 , n3)
    return a, b

elif hao == "*" + "/":
    a = str(n1) + '*' + str(n2) + "/" + str(n3) + '='
    b = n1 * Fraction(n2 , n3)
    return a, b

n = int(input('请输入你想要的题目数目: '))
k =n

开始运行函数

while k > 0:
c = [cal1(), cal2()]
a = c[random.randint(0, 1)]
b1 = list(a[0])
not_all_repeat.append(b1)
q.append(a[0])
ans.append(a[1])
k -= 1

repeat_num = 0
for i1 in range(0, n):
for i2 in range(0, n):
if i1 != i2:
if not_all_repeat[i1] == not_all_repeat[i2]:
repeat_num += 1
repeat1.append(str(i1))
repeat2.append(str(i2))
repeat_ans.append(str(q[i1]))

y = 1
for i in q:
text = open('Exercises.txt', 'a+')
text.write("题目{}: ".format(y))
text.write(i)
text.write('\n')
text.close()
y += 1

e = 1
for i in ans:
answer = open('Answer.txt', 'a+')
answer.write("第{}题答案: ".format(e))
answer.write(str(i))
answer.write('\n')
answer.close()
e += 1

e1 = 0
e2 = 0
for o in range(0, len(q)):
print(q[o])
d = o + 1
a = int(input("这是第{}题,请输入你的答案: ".format(d)))
#区分正确与错误的答案
if a == ans[o]:
e1 += 1
right.append(a)
right_num.append(d)
else:
e2 += 1
wrong.append(a)
wrong_num.append(d)

c = open('Grade.txt', 'a+')
for i in right:
c.write(str(i))
c.write("\n")

for i in wrong:
c.write(str(i))
c.write("\n")

c.write("正确的题目有 : ")
c.write(str(e1))

for i in right_num:
c.write(str(i))
c.write(" ")

c.write("\n")

c.write("错误的题目有 : ")
c.write(str(e2))

for i in wrong_num:
c.write(str(i))
c.write(" ")
c.write('\n')

c.write("重复的题目有: {} 道。".format(repeat_num))

c.write('\n')
c.write("重复的Detail: ")
c.write('\n')

for i in range (0, repeat_num):
print('第 {} 组: '.format(i + 1))
print("第 {} 题与第 {} 题重复, 题目为: {}".format(repeat1[i], repeat2[i], repeat_ans[i]))
c.write('\n')

print('请在Grade.txt里查看你的答案是否正确')
c.close()

posted @ 2021-11-20 20:58  20211309宁心宇  阅读(20)  评论(0编辑  收藏  举报