import random
import webbrowser
import os

def generate_problem():
    # 随机选题型:0-小数÷整数,1-整数÷小数,2-小数÷小数
    typ = random.choice([0, 1, 2])
    if typ == 0:
        dividend = round(random.uniform(0.1, 99.9), 1)
        divisor = random.randint(1, 9)
    elif typ == 1:
        dividend = random.randint(1, 100)
        divisor = round(random.uniform(0.1, 9.9), 1)
        while divisor == 0:
            divisor = round(random.uniform(0.1, 9.9), 1)
    else:
        dividend = round(random.uniform(0.1, 99.9), 1)
        divisor = round(random.uniform(0.1, 9.9), 1)
        while divisor == 0:
            divisor = round(random.uniform(0.1, 9.9), 1)

    # 格式化数字,去掉多余的 .0
    def fmt(num):
        return str(int(num)) if num.is_integer() else str(num)

    num1 = fmt(dividend)
    num2 = fmt(divisor)

    # 用 span 分别设置字体和字号
    # 数字:Times New Roman, 四号 (14pt)
    # 除号:楷体, 五号 (10.5pt)
    html_problem = (f'<span style="font-family: Times New Roman, serif; font-size: 14pt;">{num1}</span>'
                    f'<span style="font-family: 楷体, KaiTi, serif; font-size: 10.5pt;">÷</span>'
                    f'<span style="font-family: Times New Roman, serif; font-size: 14pt;">{num2}</span>')
    return html_problem

# 生成500道题,每行一个,带编号
problems_html = []
for i in range(1, 501):
    problems_html.append(f'<p style="margin: 4px 0;">{i}. {generate_problem()}</p>')

# 构建完整 HTML 页面
html_content = f"""<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>五年级小数除法500题</title>
    <style>
        body {{
            font-family: "Times New Roman", serif;
            padding: 20px;
            max-width: 900px;
            margin: auto;
            line-height: 1.6;
        }}
        h2 {{
            text-align: center;
            font-family: "Times New Roman", serif;
            font-size: 18pt;
        }}
        .problem {{
            font-size: 14pt;  /* 整体基准,但数字和除号已单独指定 */
        }}
    </style>
</head>
<body>
    <h2>五年级小数除法 500 题</h2>
    <div class="problem">
        {''.join(problems_html)}
    </div>
</body>
</html>
"""

# 写入文件
filename = "division_500.html"
with open(filename, "w", encoding="utf-8") as f:
    f.write(html_content)

# 自动打开浏览器预览
webbrowser.open(filename)

print(f"已生成 {filename},已用浏览器打开。您可以直接打印该页面(字体已按要求设置)。")

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: