Python实现自动生成小学四则运算题目
GitHub仓库地址:https://github.com/cherry43002/python
1、 需求分析:
- 能自动生成小学四则运算题目(注意是给小学生用的,要是结果出现负数的话他们会迷茫的!)
- 除了整数外,还要支持真分数的四则运算
2、 功能设计:
(1) 基础功能:实现四则运算题目的自动生成,并打印出题目的答案
(2) 扩展功能:实现菜单操作
(3) 高级功能:
3、 设计实现:
4、代码说明:
1 import random 2 3 from fractions import Fraction 4 5 def newint(): 6 opr = ['+', '-', '×', '÷'] 7 fh = random.randint(0, 3) 8 n1 = random.randint(1, 20) 9 n2 = random.randint(1, 20) 10 rjg = 0 11 if fh == 0: 12 rjg = n1 + n2 13 elif fh == 1: 14 n1, n2 = max(n1, n2), min(n1, n2) 15 rjg = n1 - n2 16 elif fh == 2: 17 rjg = n1 * n2 18 elif fh == 3: 19 n1, n2 = max(n1, n2), min(n1, n2) 20 while n1 % n2 != 0: 21 n1 = random.randint(1, 10) 22 n2 = random.randint(1, 10) 23 n1, n2 = max(n1, n2), min(n1, n2) 24 rjg = int(n1 / n2) 25 print(n1, opr[fh], n2, '= ', end='') 26 return rjg 27 28 29 30 def newfra(): 31 opr = ['+', '-', '×', '÷'] 32 fh = random.randint(0, 3) 33 t1 = random.randint(1, 10) 34 t2 = random.randint(t1, 10) 35 n1 = Fraction(t1, t2) 36 t1 = random.randint(1, 10) 37 t2 = random.randint(t1, 10) 38 n2 = Fraction(t1, t2) 39 rjg = 0 40 if fh == 0: 41 rjg = n1 + n2 42 elif fh == 1: 43 n1, n2 = max(n1, n2), min(n1, n2) 44 rjg = n1 - n2 45 elif fh == 2: 46 rjg = n1 * n2 47 elif fh == 3: 48 n1, n2 = max(n1, n2), min(n1, n2) 49 rjg = n1 / n2 50 print(n1, opr[fh], n2, '= ', end='') 51 return rjg 52 53 def newtest(): 54 opr = ['+', '-', '×', '÷'] 55 print('输入题库所需要的题目数量') 56 n=int(input()) 57 rjg=[] 58 m=0 59 while m<=(n-1): 60 fh = random.randint(0, 4) 61 if fh==0: 62 print(m+1,end='、') 63 rjg.append(newfra()) 64 print(' ') 65 else: 66 print(m+1,end='、') 67 rjg.append(newint()) 68 print(' ') 69 m=m+1 70 m=0 71 print('答案:') 72 while m<=(n-1): 73 print(m+1,'、',rjg[m]) 74 m=m+1 75 76 77 78 print('1、四则运算') 79 print('2、制作题库') 80 n=int(input()) 81 if n==1: 82 print('input "0000" to Quit') 83 while True: 84 fh = random.randint(0, 4) 85 if fh == 0: 86 rjg = newfra() 87 jg = input() 88 if jg == '0000': 89 break; 90 sr = Fraction(jg) 91 if sr == rjg: 92 print('right') 93 else: 94 print('error. the Tight answer is', rjg) 95 else: 96 rjg = newint() 97 jg = input() 98 if jg == '0000': 99 break; 100 sr = int(jg) 101 if sr == rjg: 102 print('right') 103 else: 104 print('error. the Tight answer is', rjg) 105 if n==2: 106 newtest()
5、运行测试:
6、PSP
7、小结
1、 知识水平有限,对问题理解不够透彻,需提高自身知识素养
2、 对Python编程语言不够熟练,今后加强练习
3、 对撰写论文还属于初级阶段,今后多写多练