Python_小学口算题库生成器
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python_小学口算题库生成器相关的知识,希望对你有一定的参考价值。
1 import random 2 import os 3 import tkinter 4 import tkinter.ttk 5 from docx import Document 6 columnsNumber = 4 7 def main(rowsNumbers=20,grade=4): 8 if grade < 3: 9 operators = ‘+-‘ 10 biggest = 20 11 elif grade <= 4: 12 operators = ‘+-×÷‘ 13 biggest = 100 14 elif grade == 5: 15 operators = ‘+-×÷(‘ 16 biggest = 100 17 18 document = Document() 19 table = document.add_table(rows=rowsNumbers,cols=columnsNumber) 20 table.style.font.name = ‘宋体‘ 21 for row in range(rowsNumbers): 22 for col in range(columnsNumber): 23 first = random.randint(1,biggest) 24 second = random.randint(1,biggest) 25 operator = random.choice(operators) 26 27 if operator != ‘(‘: 28 if operator == ‘-‘: 29 if first < second: 30 first,second = second,first 31 r = str(first).ljust(2,‘ ‘)+‘ ‘ + operator + str(second).ljust(2,‘ ‘) + ‘=‘ 32 else: 33 third = random.randint(1,100) 34 while True: 35 o1 = random.choice(operators) 36 o2 = random.choice(operators) 37 if o1 != ‘(‘ and o2 !=‘(‘: 38 break 39 rr = random.randint(1,100) 40 if rr > 50: 41 if o2 == ‘-‘: 42 if second < third: 43 second,third = third,second 44 r = str(first).ljust(2,‘ ‘) + o1 +‘(‘+str(second).ljust(2,‘ ‘)+o2+str(third).ljust(2,‘ ‘)+‘)=‘ 45 else: 46 if o1 == ‘-‘: 47 if first < second: 48 first,second = second,first 49 r = ‘(‘ + str(first).ljust(2,‘ ‘) + o1 +str(second).ljust(2,‘ ‘)+‘)‘+o2+str(third).ljust(2,‘ ‘)+‘=‘ 50 cell = table.cell(row,col) 51 cell.text = r 52 document.save(‘kousuan.docx‘) 53 54 if __name__ == ‘__main__‘: 55 app = tkinter.Tk() 56 app.title(‘KouSuan.zWrite‘) 57 app[‘width‘] = 300 58 app[‘height‘] = 150 59 lableNumber = tkinter.Label(app,text=‘Number:‘,justify=tkinter.RIGHT,width=50) 60 lableNumber.place(x=10,y=40,width=50,height=20) 61 comboNumber = tkinter.ttk.Combobox(app,values=(100,200,300,400,500),width=50) 62 comboNumber.place(x=70,y=40,width=50,height=20) 63 64 labelGrade = tkinter.Label(app,text=‘Grade:‘,justify=tkinter.RIGHT,width=50) 65 labelGrade.place(x=130,y=40,width=50,height=20) 66 comboGrade = tkinter.ttk.Combobox(app,values=(1,2,3,4,5),width=50) 67 comboGrade.place(x=200,y=40,width=50,height=20) 68 69 def generate(): 70 number = int(comboNumber.get()) 71 grade = int(comboGrade.get()) 72 main(number,grade) 73 buttonGenerate = tkinter.Button(app,text=‘=‘,width=40,command=generate) 74 buttonGenerate.place(x=130,y=90,width=40,height=30) 75 76 app.mainloop()
以上是关于Python_小学口算题库生成器的主要内容,如果未能解决你的问题,请参考以下文章