码迷,mamicode.com
首页 > 其他好文 > 详细

小学四则运算编程实践

时间:2020-12-03 12:26:32      阅读:16      评论:0      收藏:0      [点我收藏+]

标签:lse   答案   alt   from   运算   code   while   append   lazy   

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

 

技术图片

技术图片

小学四则运算编程实践

标签:lse   答案   alt   from   运算   code   while   append   lazy   

原文地址:https://www.cnblogs.com/20201212ycy/p/14056941.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!