使用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}.")
相关推荐
用户298698530146 分钟前
Python 一键导出 Excel 中的图表和形状为图片
后端·python·excel
汤米粥10 分钟前
Python爬虫——json解析
爬虫·python·json
武陵悭臾25 分钟前
Python应用开发学习:如何让随机数按一定比列出现
python·学习·random·概率·随机数
amongdata42 分钟前
MyVoiceTyping:面向中文语音输入的本地优先开源方案
python·开源·音视频·语音识别
薛定猫AI44 分钟前
【技术干货】大模型检查点可靠性评测:基于 Python 构建幻觉与指令遵循测试
开发语言·python
Mr__Miss1 小时前
Java 随机数生成器选型指南:Random、ThreadLocalRandom 与 SecureRandom
java·开发语言·python
CTA量化套保1 小时前
2026年下半年Python量化入门,先看API如何接进小流程
人工智能·python
薛定猫AI2 小时前
【深度解析】动态多智能体团队:并行规划、开发与验证的 Python 实战
大数据·开发语言·python
yaoxin52112313 小时前
462. Java 反射 - 获取声明类与封闭类
java·开发语言·python
中微极客13 小时前
解锁LLM开发全栈能力:Python + LangChain + RAG 工程实战指南
人工智能·python·langchain