在小学一、二年级,只能口算 20 以内整数的加、减法;三、四年级可以口 算超过 20 的整数四则运算;五年级以上可以口算带括号的式子。 编写程序,批量生成小学口算题,要求把生成的口算题写入 Word 文件中 的表格。表格共 4 列,用户指定表格行数和题目适用年级。程序运行后界面如 下图所示。
import random
import os
import tkinter
import tkinter.ttk
from docx.shared import Pt
from docx import Document
columnsNumber = 4
def main(rowsNumbers,grade):
if grade < 3:
operators = '+-'
Max = 20
elif grade <= 4:
operators = '+-×÷'
Max = 100
elif grade == 5:
operators = '+-×÷('
Max = 100
document = Document()
table = document.add_table(rows=rowsNumbers,cols=columnsNumber)
table.style.font.name = '微软雅黑'
table.style.font.size =Pt(10)
for row in range(rowsNumbers):
for col in range(columnsNumber):
first = random.randint(1,Max)
second = random.randint(1,Max)
operator = random.choice(operators)
if operator != '(': #不是五年级
if operator == '-'or'÷':
if first < second:
first,second = second,first
r = str(first).ljust(2,' ')+' ' + operator + str(second).ljust(2,' ') + '='
else: #是五年级
third = random.randint(1,100)
while True:
o1 = random.choice(operators)
o2 = random.choice(operators)
if o1 != '(' and o2 !='(':
break
#考虑括号的口算题
r2 = random.randint(1,100)
if r2 > 50:
if o2 == '-':
if second < third:
second,third = third,second
r = str(first).ljust(2,' ') + o1 +'('+str(second).ljust(2,' ')+o2+str(third).ljust(2,' ')+')='
else:
if o1 == '-':
if first < second:
first,second = second,first
r = '(' + str(first).ljust(2,' ') + o1 +str(second).ljust(2,' ')+')'+o2+str(third).ljust(2,' ')+'='
cell = table.cell(row,col)
cell.text = r
document.save('小学生口算题.docx')
os.startfile("小学生口算题.docx")
if __name__ == '__main__':
app = tkinter.Tk()
app.title('小学生口算题生成器')
app['width'] = 300
app['height'] = 150
lableNumber = tkinter.Label(app,text='Number:',justify=tkinter.RIGHT,width=50)
lableNumber.place(x=10,y=40,width=50,height=20)
comboNumber = tkinter.ttk.Combobox(app,values=(100,200,300,400,500),width=50)
comboNumber.place(x=70,y=40,width=50,height=20)
labelGrade = tkinter.Label(app,text='Grade:',justify=tkinter.RIGHT,width=50)
labelGrade.place(x=130,y=40,width=50,height=20)
comboGrade = tkinter.ttk.Combobox(app,values=(1,2,3,4,5),width=50)
comboGrade.place(x=200,y=40,width=50,height=20)
def generate():
number = int(comboNumber.get())
grade = int(comboGrade.get())
rowsNumbers = int(number/4)
main(rowsNumbers,grade)
buttonGenerate = tkinter.Button(app,text='Go',width=40,command=generate)
buttonGenerate.place(x=130,y=90,width=40,height=30)
app.mainloop()
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)