编辑代码

from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import A4
from reportlab.lib.units import cm
import random


def generate_multiplication_or_division():
    num1 = random.randint(1, 9)
    num2 = random.randint(1, 9)
    operation = random.choice(['×', '÷'])
    if operation == '×':
        question = f"{num1} {operation} {num2} ="
        answer = num1 * num2
    else:
        if num1 % num2!= 0:
            num1 = num2 * random.randint(1, 9)
        question = f"{num1} {operation} {num2} ="
        answer = num1 // num2
    return question


def generate_mixed_operation():
    num1 = random.randint(1, 9)
    num2 = random.randint(1, 9)
    num3 = random.randint(1, 9)
    operator1 = random.choice(['+', '-', '×'])
    operator2 = random.choice(['+', '-', '×'])
    question = f"{num1} {operator1} {num2} {operator2} {num3} ="
    if operator1 == '+':
        if operator2 == '+':
            answer = num1 + num2 + num3
        elif operator2 == '-':
            answer = num1 + num2 - num3
        elif operator2 == '×':
            answer = num1 + num2 * num3
    elif operator1 == '-':
        if operator2 == '+':
            answer = num1 - num2 + num3
        elif operator2 == '-':
            answer = num1 - num2 - num3
        elif operator2 == '×':
            answer = num1 - num2 * num3
    elif operator1 == '×':
        if operator2 == '+':
            answer = num1 * num2 + num3
        elif operator2 == '-':
            answer = num1 * num2 - num3
        elif operator2 == '×':
            answer = num1 * num2 * num3
    return question


def generate_parentheses_operation():
    num1 = random.randint(1, 9)
    num2 = random.randint(1, 9)
    num3 = random.randint(1, 9)
    operator1 = random.choice(['+', '-', '×'])
    operator2 = random.choice(['+', '-', '×'])
    question = f"({num1} {operator1} {num2}) {operator2} {num3} ="
    if operator1 == '+':
        if operator2 == '+':
            answer = (num1 + num2) + num3
        elif operator2 == '-':
            answer = (num1 + num2) - num3
        elif operator2 == '×':
            answer = (num1 + num2) * num3
    elif operator1 == '-':
        if operator2 == '+':
            answer = (num1 - num2) + num3
        elif operator2 == '-':
            answer = (num1 - num2) - num3
        elif operator2 == '×':
            answer = (num1 - num2) * num3
    elif operator1 == '×':
        if operator2 == '+':
            answer = (num1 * num2) + num3
        elif operator2 == '-':
            answer = (num1 * num2) - num3
        elif operator2 == '×':
            answer = (num1 * num2) * num3
    return question


def generate_test_paper():
    width, height = A4
    c = canvas.Canvas("test_paper.pdf", pagesize = A4)
    # 标题
    c.setFont("Helvetica - Bold", 16)
    c.drawString(3 * cm, height - 2 * cm, "二年级(上)口算练习")
    c.setFont("Helvetica", 12)
    c.drawString(3 * cm, height - 3 * cm, "班级:______ 姓名:______ 学号:______")
    y_position = height - 4 * cm
    # 乘法与除法混合
    c.drawString(3 * cm, y_position, "1. 乘法与除法混合")
    y_position -= 1.5 * cm
    for _ in range(20):
        question = generate_multiplication_or_division()
        c.drawString(3 * cm, y_position, question)
        y_position -= 0.7 * cm
    # 混合运算
    c.drawString(3 * cm, y_position, "2. 混合运算")
    y_position -= 1.5 * cm
    for _ in range(10):
        question = generate_mixed_operation()
        c.drawString(3 * cm, y_position, question)
        y_position -= 0.7 * cm
    # 带括号运算
    c.drawString(3 * cm, y_position, "3. 带括号运算")
    y_position -= 1.5 * cm
    for _ in range(10):
        question = generate_parentheses_operation()
        c.drawString(3 * cm, y_position, question)
        y_position -= 0.7 * cm
    c.save()


if __name__ == "__main__":
    generate_test_paper()