使用Python生成100到算术题

需求描述:生成100道包含加法、减法、乘法、除法题目的数学题。

python 复制代码
import random
import pandas as pd

def generate_math_questions(num_questions, question_types=['addition', 'subtraction', 'multiplication', 'division'], difficulty='easy', range_start=1, range_end=10):
    """
    生成数学题目的函数。

    Args:
        num_questions (int): 要生成的题目数量。
        question_types (list): 题目类型的列表,默认为 ['addition', 'subtraction', 'multiplication', 'division']。
        difficulty (str): 题目的难度,默认为 'easy'。
        range_start (int): 题目中数字的范围的起始值,默认为 1。
        range_end (int): 题目中数字的范围的结束值,默认为 10。

    Returns:
        list: 包含生成的题目和答案的元组列表。
    """
    questions = []

    for _ in range(num_questions):
        # 随机选择题目类型
        question_type = random.choice(question_types)
        if question_type == 'addition':
            # 生成加法题目
            operand1 = random.randint(range_start, range_end)
            operand2 = random.randint(range_start, range_end)
            answer = operand1 + operand2
            question = f"What is {operand1} + {operand2}?"
        elif question_type == 'subtraction':
            # 生成减法题目
            operand1 = random.randint(range_start, range_end)
            operand2 = random.randint(range_start, operand1)
            answer = operand1 - operand2
            question = f"What is {operand1} - {operand2}?"
        elif question_type == 'multiplication':
            # 生成乘法题目
            operand1 = random.randint(range_start, range_end)
            operand2 = random.randint(range_start, range_end)
            answer = operand1 * operand2
            question = f"What is {operand1} * {operand2}?"
        elif question_type == 'division':
            # 生成除法题目
            answer_found = False
            while not answer_found:
                operand1 = random.randint(range_start, range_end)
                operand2 = random.randint(range_start, range_end)
                if operand2 != 0 and operand1 % operand2 == 0:
                    answer = operand1 // operand2
                    question = f"What is {operand1} / {operand2}?"
                    answer_found = True
        questions.append((question, answer))

    return questions

# 生成100道包含加法、减法、乘法、除法题目的数学题
math_questions = generate_math_questions(100, question_types=['addition', 'subtraction', 'multiplication', 'division'], range_start=1, range_end=20)

# 输出题目
for i, (question, answer) in enumerate(math_questions, start=1):
    print(f"Question {i}: {question}")

# 输出答案
for i, (question, answer) in enumerate(math_questions, start=1):
    print(f"Answer {i}: {answer}")

# 将题目和答案组织成DataFrame
df = pd.DataFrame(math_questions, columns=['Question', 'Answer'])

# 将DataFrame写入Excel文件
excel_file_path = "math_questions.xlsx"
df.to_excel(excel_file_path, index=False)

print(f"Math questions and answers are saved to {excel_file_path}.")
相关推荐
中国loong几秒前
pandas连接mysql数据库
python
带娃的IT创业者28 分钟前
《Python实战进阶》专栏 No.3:Django 项目结构解析与入门DEMO
数据库·python·django
HealthScience1 小时前
【异常错误】pycharm debug view变量的时候显示不全,中间会以...显示
ide·python·pycharm
豌豆花下猫2 小时前
Python 潮流周刊#90:uv 一周岁了,优缺点分析(摘要)
后端·python·ai
橘猫云计算机设计2 小时前
基于SSM的《计算机网络》题库管理系统(源码+lw+部署文档+讲解),源码可白嫖!
java·数据库·spring boot·后端·python·计算机网络·毕设
小伍_Five2 小时前
从0开始:OpenCV入门教程【图像处理基础】
图像处理·python·opencv
m0_748245342 小时前
python——Django 框架
开发语言·python·django
java1234_小锋3 小时前
一周学会Flask3 Python Web开发-客户端状态信息Cookie以及加密
前端·python·flask·flask3
B站计算机毕业设计超人4 小时前
计算机毕业设计Python+DeepSeek-R1高考推荐系统 高考分数线预测 大数据毕设(源码+LW文档+PPT+讲解)
大数据·python·机器学习·网络爬虫·课程设计·数据可视化·推荐算法
winfredzhang4 小时前
Python实战:Excel中文转拼音工具开发教程
python·安全·excel·汉字·pinyin·缩写