马上2024年放寒假了,用python开发了一个,小学加减乘除算术练习题自动出题工具,可以自动出题,自动判断对错。
- from tkinter import *
- import random
-
- def jiajian():# 加减法函数,生成加数,和,被减数,减数,差
- joj=random.randint(1,2)
- a = random.randint(1, 999)
- b = random.randint(1, 999)
- if joj==1: #随机产生
- h=a+b
- return [a,'+',b,h]
- else:
- if a > b:
- h=a-b
- return [a,'-',b,h]
- else:
- h=b-a
- return [b,'-',a,h]
-
- def cc(i=1):
- #乘除运算,返回乘数,积。i表示难度,1为个位数,2为2位数,3为3位数,j,1是乘,2是除
- jc = random.randint(1, 2)
- if i==1: #难度1
- a=random.randint(2,9)
- b = random.randint(2, 9)
- j=a*b
- if jc==1:#乘法
- return [a,'*',b,j] #1为乘法,2为除法
- else:
- return [j,'/',a,b] #1为乘法,2为除法
- elif i==2: #难度2
- a = random.randint(11, 99)
- b = random.randint(11, 99)
- j = a * b
- if jc == 1: # 乘法
- return [a,'*', b, j] # 1为乘法,2为除法
- else:
- return [j, '/', a, b] # 1为乘法,2为除法
- else: #难度3
- a = random.randint(101, 999)
- b = random.randint(101, 999)
- j = a * b
- if jc == 1: # 乘法
- return [a, '*', b, j] # 1为乘法,2为除法
- else:
- return [j, '/', a, b] # 1为乘法,2为除法
-
- def yz():#这是按钮的验证函数。
- global lista
- if ent1.get()==str(lista[3]):
- ndsel()
- lab1.config(text=str(lista[0])+lista[1]+str(lista[2])+'=')
- lab2.config(text='上一题解题正确,已更新题目')
- lab2.config(fg='green')
- ent1.delete(0,END)
- ent1.focus()
- else:#回答错误,清除文本框,标签2更新
- ent1.delete(0,END)
- ent1.focus()
- lab2.config(text='解答错误请重新输入')
- lab2.config(fg='red')
-
- def ndsel():#单选按钮难度设定
- global lista
- a=nd.get()
- if a == 1:
- lista=jiajian()
- lab1.config(text=str(lista[0]) + lista[1] + str(lista[2]) + '=')
- lab2.config(text='题目已变更')
- lab2.config(fg='green')
- ent1.delete(0, END)
- ent1.focus()
- else:
- lista=cc(a-1)
- lab1.config(text=str(lista[0]) + lista[1] + str(lista[2]) + '=')
- lab2.config(text='题目已变更')
- lab2.config(fg='green')
- ent1.delete(0, END)
- ent1.focus()
-
- lista=jiajian()
- root=Tk()
- root.title("小学加减乘除练习")
- root.geometry('600x400')
- labf1=LabelFrame(root,text='模式选择',width=500,height=150)
- labf1.pack()
- nd=IntVar()
- nd.set(1)
- rb1=Radio<button(labf1,text='加减法运算',variable=nd,value=1,command=ndsel)
- rb2=Radio<button(labf1,text='十以内乘除法',variable=nd,value=2,command=ndsel)
- rb3=Radio<button(labf1,text='百以内乘除法',variable=nd,value=3,command=ndsel)
- rb4=Radio<button(labf1,text='千以内乘除法',variable=nd,value=4,command=ndsel)
- rb1.pack(side=LEFT)
- rb2.pack(side=LEFT)
- rb3.pack(side=LEFT)
- rb4.pack(side=LEFT)
- lab1=Label(root,text=str(lista[0])+lista[1]+str(lista[2])+'=',font=('宋体',28))
- lab1.pack()
- ent1=Entry(root,font=('宋体',28),width=10)
- ent1.pack()
- but1=Button(root,text='确定',font=('宋体',28),command=yz)
- but1.pack()
- lab2=Label(root,text='请在文本框内输入正确答案,点击确定键验算',pady=5)
- lab2.pack()
- '''f=open('a.txt','w',encoding='utf8')
- for i in range(10000):
- listb=jiajian()
- f.write(str(listb[0])+listb[1]+str(listb[2])+'='+"ttt")
- f.close()'''
- root.mainloop()