小学四则运算编程实践

第一次编写个人项目,基本实现所给要求,可实现10000道题目与答案的生成,但不能实现对答案正误的检测,可以检测出生成题目中完全相同的题目并统计出数目,但对有些变化的重复题目无法检测,可以输出具体的重复的题目,到无法输出题号。

点击查看代码

with open('题目.txt','w+') as f1:
    f1.writelines('小学四则运算题目:') 
with open('答案.txt','w+') as f2:
    f2.writelines('参考答案:')
    
list1 = []
list2 = []
list3 = []

def cal(n):
    b = random.randint(1,10)
    a = random.randint(1,10)
    c = random.randint(1,10)
    d = random.randint(1,10)
    t = random.randint(1,10)
    w = random.randint(1,10)
    if n == 1:
        e = '{0} + {1} ='.format(a,b)
        f = str(a + b)
    elif n == 2:
        if b > a:
            a,b = b,a
        e = '{0} - {1} ='.format(a,b)
        f = str(a - b)
    elif n == 3:
        e = '{0} * {1} ='.format(a,b)
        f = str(a * b)
    elif n == 4:
        e = '{0} / {1} ='.format(a,b)
        f = str(a / b)
    elif n == 5:
        if a > b:
            a,b = b,a
        if c > d:
            c,d = d,c
        e = '{0} / {1} + {2} / {3} ='.format(a,b,c,d)
        f = str(a/b + c/d)
    elif n == 6:
        if a > b:
            a,b = b,a
        if c > d:
            c,d = d,c
        if c/d > a/b:
            a,c = c,a
            b,d = d,b
        e = '{0} / {1} - {2} / {3} ='.format(a,b,c,d)
        f = str(a/b - c/d)  
    elif n == 7:
        if a > b:
            a,b = b,a
        if c > d:
            c,d = d,c
        e = '({0} / {1}) * ({2} / {3}) ='.format(a,b,c,d)
        f = str((a/b) * (c/d))
    elif n == 8:
        if a > b:
            a,b = b,a
        if c > d:
            c,d = d,c
        e = '({0} / {1}) / ({2} / {3}) ='.format(a,b,c,d)
        f = str((a/b) / (c/d))
    elif n == 9:
        if a > b:
            a,b = b,a
        if c > d:
            c,d = d,c
        e = '{0}’{1} / {2} + {3} / {4} ='.format(w,a,b,c,d)
        f = str((w+a/b) + c/d)
    elif n == 10:
        if a > b:
            a,b = b,a
        if c > d:
            c,d = d,c
        if c/d > a/b:
            a,c = c,a
            b,d = d,b
        e = '{0}’{1} / {2} - {3} / {4} ='.format(w,a,b,c,d)
        f = str((w+a/b) - c/d)  
    elif n == 11:
        if a > b:
            a,b = b,a
        if c > d:
            c,d = d,c
        e = '{0}’({1} / {2}) * ({3} / {4}) ='.format(w,a,b,c,d)
        f = str((w+(a/b)) * (c/d))
    elif n == 12:
        if a > b:
            a,b = b,a
        if c > d:
            c,d = d,c
        e = '({0}’{1} / {2}) / ({3} / {4}) ='.format(w,a,b,c,d)
        f = str((w+(a/b)) / (c/d))       
    elif n == 13:
        if c > d:
            c,d = d,c
        e = '{0} + {1} / {2} ='.format(a,c,d)
        f = str(a + c / d)
    elif n == 14:
        if c > d:
            c,d = d,c
        if c / d > a:
            a,c = c,a
        e = '{0} - {1} / {2} ='.format(a,c,d)
        f = str(a - c / d)
    elif n == 15:
        if c > d:
            c,d = d,c
        e = '{0} * ({1} / {2}) ='.format(a,c,d)
        f = str(a * (c / d))
    elif n == 16:
        if c > d:
            c,d = d,c
        e = '{0} / ({1} / {2}) ='.format(a,c,d)
        f = str(a / (c / d))
    elif n == 17:
        if a > b:
            a,b = b,a
        if c > d:
            c,d = d,c
        e = '{0}’{1} / {2} + {3}’{4} / {5} ='.format(w,a,b,t,c,d)
        f = str(w + a/b + t + c/d)
    elif n == 18:
        if a > b:
            a,b = b,a
        if c > d:
            c,d = d,c
        if t + c/d > w + a/b:
            a,c = c,a
            b,d = d,b
            t,w = w,t
        e = '{0}’{1} / {2} - {3}’{4} / {5} ='.format(w,a,b,t,c,d)
        f = str((w + a/b) - (t + c/d))  
    elif n == 19:
        if a > b:
            a,b = b,a
        if c > d:
            c,d = d,c
        e = '({0}’{1} / {2}) * ({3}’{4} / {5})) ='.format(w,a,b,t,c,d)
        f = str((w + a / b) * (t + c / d))
    elif n == 20:
        if a > b:
            a,b = b,a
        if c > d:
            c,d = d,c
        e = '({0}’{1} / {2}) / ({3}’{4} / {5})) ='.format(w,a,b,t,c,d)
        f = str((w + a / b) / (t + c / d))
    elif n == 21:
        if a > b:
            a,b = b,a
        e = '{0}’{1} / {2} + {3}='.format(w,a,b,c)
        f = str((w+a/b) + c)
    elif n == 22:
        if a > b:
            a,b = b,a
        if c > w + a / b:
            c,w = w,c
        e = '{0}’{1} / {2} - {3} ='.format(w,a,b,c)
        f = str((w+a/b) - c)  
    elif n == 23:
        if a > b:
            a,b = b,a
        e = '{0}’({1} / {2}) * {3}) ='.format(w,a,b,c)
        f = str((w+(a/b)) * c)
    elif n == 24:
        if a > b:
            a,b = b,a
        e = '({0}’{1} / {2}) / {3} ='.format(w,a,b,c)
        f = str((w+(a/b)) / c)    
    elif n == 25:
        if c > d:
            c,d = d,c
        e = '{0} + {1}’{2} / {3} = '.format(a,w,c,d)
        f = str(a + w + c / d)
    elif n == 26:
        if c > d:
            c,d = d,c
        if w + c / d > a:
            a,w = w,a
            a += 2
        e = '{0} - {1}’{2} / {3} = '.format(a,w,c,d)
        f = str(a - w + c / d)
    elif n == 27:
        if c > d:
            c,d = d,c
        e = '{0} * ({1}’{2} / {3}) = '.format(a,w,c,d)
        f = str(a * (w + c / d))
    elif n == 28:
        if c > d:
            c,d = d,c
        e = '{0} / ({1}’{2} / {3}) = '.format(a,w,c,d)
        f = str(a / (w + c / d))
    elif n == 29:
        if c > d:
            c,d = d,c
        if a > b:
            a,b = b,a
        e = '({0} / {1}) + ({2}’{3} / {4}) = '.format(a,b,w,c,d)
        f = str((a / b) + (w + c / d))
    elif n == 30:
        if c > d:
            c,d = d,c
        if a > b:
            a,b = b,a
        e = '({0} / {1}) * ({2}’{3} / {4}) = '.format(a,b,w,c,d)
        f = str((a / b) * (w + c / d))
    elif n == 31:
        if c > d:
            c,d = d,c
        if a > b:
            a,b = b,a
        e = '({0} / {1}) / ({2}’{3} / {4}) = '.format(a,b,w,c,d)
        f = str((a / b) / (w + c / d))
    elif n == 32:
        if a > b:
            a,b = b,a
        e = '{0} / {1} + {2}='.format(a,b,c)
        f = str((a/b) + c)        
    elif n == 33:
        if a > b:
            a,b = b,a
        e = '({0} / {1}) * {2}='.format(a,b,c)
        f = str((a/b) * c)
    else:
        if a > b:
            a,b = b,a
        e = '({0} / {1}) / {2}='.format(a,b,c)
        f = str((a/b) / c)
    with open('答案.txt','a+') as f2:
        f2.writelines('\n')
        f2.writelines(f)
    times = 0
    for i in list1:
        if e == i:
            list2.append(e)
            times += 1
            list3.append(times)
    list1.append(e)
    return e

n = int(input('请输入你想做题的数目:'))
while n != 0:
    m = random.randint(1,34)
    question =cal(m)
    print(question)
    with open('题目.txt','a+') as f1:
        f1.writelines('\n')
        f1.writelines(question)
    n -= 1

def f1():
    z = 0
    for i in list3:
        z += 1   
    return z
times = f1()

print('Repeat:{0}'.format(times))
print('RepeatDetail:',list2)



posted @ 2021-11-18 22:14  20211409赵枢博  阅读(27)  评论(0编辑  收藏  举报