生成小学计算题(升级版)
一、Github项目地址:
代码还在完善中,升级完成后再提交至GitHub
二、各模块耗费时间估计
模块 |
耗费时间(单位:h) |
项目要求分析 |
0.5 |
编写代码 |
1 |
项目测试及优化 |
3 |
博客撰写 |
2 |
三、解题思路
由于题目属于基本的小学算术题,只需满足输出题目,输入答案,并输出验证成果,在此基础上在进行一定的升级:功能升级(对一次答题的成果进行总结,如准确率,错题展示)、界面升级(采用窗口界面显示,用拖动的滑块来确定题目难度)。
四、设计过程
(1)使用者输入一个整数确定出题数目;
(2)使用者拖动滑块以确定本次出题难度,共分为1~6级;
(3)根据使用者输入数据来出题;
(4)输入学生自己的计算成果并验证其对错,并以弹窗形式反馈;
(5)升级还在继续......
五、代码说明
1 # -*- coding: utf-8 -*- 2 """ 3 Created on Sun Sep 27 10:05:41 2020 4 5 @author: Regan 6 """ 7 8 from math import * 9 from random import * 10 from fractions import * 11 import tkinter as tk 12 import tkinter.messagebox 13 14 def plus(num1, num2, fomula): 15 num1 = num1 + num2 16 fomula += '+' + str(num2) 17 return num1, fomula 18 19 def subtract(num1, num2, fomula): 20 num1 = num1 - num2 21 fomula += '-' + str(num2) 22 return num1, fomula 23 24 def multiply(num1, num2, fomula): 25 num1 = num1 * num2 26 fomula += '×' + str(num2) 27 return num1, fomula 28 29 def divide(num1, num2, fomula): 30 num1 = Fraction(num1, num2) 31 fomula += '÷' + str(num2) 32 return num1, fomula 33 34 def level1(num1, num2, fomula): 35 flag = randint(1,2) 36 if flag == 1: 37 num1, fomula = plus(num1, num2, fomula) 38 elif flag == 2: 39 num1, fomula = subtract(num1, num2 ,fomula) 40 return num1, fomula 41 42 def level2(num1, num2, fomula): 43 flag = randint(1,3) 44 if flag == 1: 45 num1, fomula = plus(num1, num2, fomula) 46 elif flag == 2: 47 num1, fomula = subtract(num1, num2 ,fomula) 48 elif flag == 3: 49 num1, fomula = multiply(num1, num2 ,fomula) 50 return num1, fomula 51 52 def level3(num1, num2, fomula): 53 flag = randint(1,4) 54 if flag == 1: 55 num1, fomula = plus(num1, num2, fomula) 56 elif flag == 2: 57 num1, fomula = subtract(num1, num2 ,fomula) 58 elif flag == 3: 59 num1, fomula = multiply(num1, num2 ,fomula) 60 elif flag == 4: 61 num1, fomula = divide(num1, num2 ,fomula) 62 return num1, fomula 63 64 def fun(right, level, false): 65 if level == 1: 66 a=[randint(1,10) for i in range(2)] 67 fomula = str(a[0]) 68 total, fomula = level1(a[0], a[1], fomula) 69 elif level == 2: 70 a=[randint(1,100) for i in range(2)] 71 fomula = str(a[0]) 72 total, fomula = level1(a[0], a[1], fomula) 73 elif level == 3: 74 a=[randint(1,10) for i in range(3)] 75 fomula = str(a[0]) 76 total = a[0] 77 for i in range(2): 78 total, fomula = level2(total, a[i+1], fomula) 79 elif level == 4: 80 a=[randint(1,100) for i in range(3)] 81 fomula = str(a[0]) 82 total = a[0] 83 for i in range(2): 84 total, fomula = level2(total, a[i+1], fomula) 85 elif level == 5: 86 a=[randint(1,10) for i in range(4)] 87 fomula = str(a[0]) 88 total = a[0] 89 for i in range(3): 90 total, fomula = level3(total, a[i+1], fomula) 91 elif level == 6: 92 a=[randint(1,100) for i in range(4)] 93 fomula = str(a[0]) 94 total = a[0] 95 for i in range(3): 96 total, fomula = level3(total, a[i+1], fomula) 97 fomula += '=' 98 return right, fomula, total 99 100 def windows(): 101 window = tk.Tk() 102 window.title("小学计算题出题程序") 103 window.geometry('500x500') 104 var = tk.StringVar() # 将label标签的内容设置为字符类型,用var来接收hit_me函数的传出内容用以显示在标签上 105 on_hit = False 106 def hit_me(): 107 e6.insert(tk.INSERT, false) 108 109 def chu_ti(): 110 level = s.get() 111 global total 112 global times 113 global fomula 114 e3.delete('1.0','end') 115 right, fomula, total = fun(0, level, '') 116 e3.insert(tk.INSERT, fomula) 117 118 def jian_yan(): 119 global false 120 ans = e4.get() 121 times = 0 122 if ans == str(total): 123 tk.messagebox.showinfo(title='结果', message='回答正确') 124 right += 1 125 else: 126 tk.messagebox.showerror(title='结果', message='回答错误') 127 times += 1 128 false += fomula + str(total) + '\n' 129 l2 = tk.Label(window, text='请选择出题难度', bg= None, font=('Arial', 12), width=30, height=2) 130 l2.place(x = 10, y = 25, width=150, height=30) 131 def print_selection(v): 132 l2.config(text='难度' + v) 133 s = tk.Scale(window, label=' 拖动滑块选择难度', from_=1, to=6, orient=tk.HORIZONTAL, length=200, showvalue=0,tickinterval=2, resolution=1, command=print_selection) 134 s.place(x = 170, y = 10, width=150, height=50) 135 b2 = tk.Button(window, text='确定', font=('Arial', 12), width=10, height=1, command=chu_ti) 136 b2.place(x = 340, y = 25, width=50, height=30) 137 138 #输出题目 139 l3 = tk.Label(window, text='出题框', bg= None, font=('Arial', 12), width=30, height=2) 140 l3.place(x = 10, y = 70, width=150, height=30) 141 e3 = tk.Text(window, font=('Arial', 14)) 142 e3.place(x = 170, y = 70, width=180, height=30) 143 144 #回答题目 145 l4 = tk.Label(window, text='答题框', bg= None, font=('Arial', 12), width=30, height=2) 146 l4.place(x = 10, y = 110, width=150, height=30) 147 e4 = tk.Entry(window, show=None, font=('Arial', 14)) 148 e4.place(x = 170, y = 110, width=180, height=30) 149 b4 = tk.Button(window, text='确定', font=('Arial', 12), width=10, height=1, command=jian_yan) 150 b4.place(x = 360, y = 110, width=50, height=30) 151 152 #答题情况总结 153 l5 = tk.Label(window, text='答题情况', bg= None, font=('Arial', 12), width=30, height=2) 154 l5.place(x = 10, y = 150, width=150, height=30) 155 e5 = tk.Text(window, show=None, font=('Arial', 14)) 156 e5.place(x = 170, y = 150, width=240, height=100) 157 158 #错题展示 159 l6 = tk.Label(window, text='错题展示', bg= None, font=('Arial', 12), width=30, height=2) 160 l6.place(x = 10, y = 260, width=150, height=30) 161 e6 = tk.Entry(window, show=None, font=('Arial', 14)) 162 e6.place(x = 170, y = 260, width=240, height=100) 163 164 #结束答题 165 b7 = tk.Button(window, text='结束答题', font=('Arial', 12), width=10, height=1, command=hit_me) 166 b7.place(x = 200, y = 400, width=100, height=50) 167 168 #主窗口循环显示 169 window.mainloop() 170 171 def main(): 172 choice = 'y' 173 right = 0 174 flase = '' 175 windows() 176 177 main()
六、测试运行
七、各模块实际耗费时间
模块 |
耗费时间(单位:h) |
项目要求分析 |
0.2 |
编写代码 |
1 |
项目测试及优化 |
4 |
博客撰写 |
1 |
由于时间问题,目前升级工作还未彻底完善,后续升级功能将尽快呈现!