def main(rowsNumber=20, grade=4): if grade < 3: operators = '+-' biggest = 20 elif grade <= 4: operators = '+-*/' biggest = 100 elif grade == 5: operators = '+-*/(' biggest = 100 document = Document() table = document.add_table(rows=rowsNumber, cols=columnsNumber) for row in range(rowsNumber): for col in range(columnsNumber): first = random.randint(1, biggest) second = random.randint(1, biggest) operator = random.choice(operators) if operator != '(': if operator == '-': 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 rr = random.randint(1, 100) if rr > 50: if o2 == '-': if second < third: second, third = third, second r = str(second).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('kousuan.docx') os.startfile('kousuan.docx')