小学四则运算训练改进

PSP表格:

 

 

 

改进思路:

小学不只是有整数的运算,还有分数的运算,在此加入分数的四则运算训练。

代码改进:

  1 import random
  2 from fractions import Fraction
  3 ##两个整数的四则运算
  4 def c1(q, ans):
  5     symbol = random.choice(['+', '-', '*', '/'])  # 生成随机符号
  6     if symbol == '+':
  7         n1 = random.randint(0, 20)
  8         n2 = random.randint(0, 20)
  9         q.append(str(n1) + '+' + str(n2) + '=')
 10         ans.append(n1 + n2)
 11     elif symbol == '-':
 12         n1 = random.randint(0, 20)
 13         n2 = random.randint(0, 20)
 14         n1,n2 = max(n1,n1),min(n1,n2)#防止出现负数
 15         q.append(str(n1) + '-' + str(n2) + '=')
 16         ans.append(n1 - n2)
 17     elif symbol == '*':
 18         n1 = random.randint(0, 20)
 19         n2 = random.randint(0, 20)
 20         q.append(str(n1) + '×' + str(n2) + '=')
 21         ans.append(n1 * n2)
 22     else:
 23         n1 = random.randint(0, 20)
 24         if n1 == 0:
 25             n2 = random.randint(1, 20)
 26         else:
 27             n2 = random.randint(1, n1 + 1)
 28         q.append(str(n1) + '÷' + str(n2) + '=')
 29         ans.append(Fraction(n1, n2))
 30 ##随机生成两个分数
 31 def createF():
 32     fz1 = random.randint(0, 20)
 33     if fz1 == 0:
 34         fm1 = random.randint(1, 20)
 35     else:
 36         fm1 = random.randint(1, 20)
 37     f1 = Fraction(fz1, fm1)
 38     fz2 = random.randint(1, 20)
 39     fm2 = random.randint(20, 20)
 40     f2 = Fraction(fz2, fm2)
 41     return f1, f2
 42 def f(f):#分数的转换
 43     a=f.numerator #分子
 44     b=f.denominator #分母
 45     if a%b==0:#为整数
 46         return '%d'%(a/b)
 47     elif a<b:#为真分数
 48         return '%d%s%d' % (a,'/',b)
 49     else:#为带分数
 50         c=int(a/b)
 51         a = a - c * b
 52         return '%d%s%d%s%d' % (c,'',a,'/',b)
 53 ##两个分数的四则运算
 54 def c2(q,ans):
 55     symbol = random.choice(['+','-','*','/'])
 56     f1,f2 = createF()
 57     if symbol =='+':
 58         while f1+f2>1:
 59             f1,f2 = createF()
 60         q.append(str(f1)+'+'+str(f2)+'=')
 61         ans.append(f1+f2)
 62     elif symbol =='-':
 63         f1,f2 = max(f1,f2),min(f1,f2)#防止出现负数
 64         q.append(str(f1)+'-'+str(f2)+'=')
 65         ans.append(f1-f2)
 66     elif symbol == '*':
 67         while f1*f2>1:
 68             f1,f2 = createF()
 69         q.append(str(f1)+'×'+str(f2)+'=')
 70         ans.append(f1*f2)
 71     else:
 72         while f1/f2>1:
 73             f1,f2=createF()
 74         q.append(str(f1)+'÷'+str(f2)+'=')
 75         ans.append(Fraction(f1,f2))
 76 def main():
 77     while 1:
 78         print("输入题目的数量", end='  ')
 79         k = int(input())
 80         p = 100 / k
 81         s = 0
 82         q = []
 83         ans = []
 84         ans2 = []
 85         for i in range(k):
 86             n = random.randint(1, 4)
 87             if n == 1:
 88                 c1(q, ans)
 89                 g = Fraction(ans[i])
 90                 ans2.append(f(g))
 91             else:
 92                 c2(q, ans)
 93                 g = Fraction(ans[i])
 94                 ans2.append(f(g))#记录带分数答案
 95         for i in range(k):
 96             print("第{}题:{}".format(i + 1, q[i]), end="  ")
 97             a = input()
 98             if a == str(ans[i]):
 99                 s = s + p
100         print("所得的分数为:{}".format(s))
101         print("正确答案:", end="  ")
102         for i in range(k):
103             if str(ans[i]) == str(ans2[i]):
104                 print(q[i] + str(ans[i]))
105             else:
106                 print("{}{}或{}".format(q[i],str(ans2[i]),str(ans[i])))
107 if __name__ == '__main__':
108     main()

 



结果展示:

 

posted on 2020-10-03 17:14  广师大01号技师  阅读(95)  评论(0编辑  收藏  举报

导航