使用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}.")
相关推荐
御承扬11 分钟前
编程素养提升之EffectivePython(Builder篇)
python·设计模式·1024程序员节
chenchihwen1 小时前
AI代码开发宝库系列:FAISS向量数据库
数据库·人工智能·python·faiss·1024程序员节
AI视觉网奇2 小时前
json 可视化 2025 coco json
python·1024程序员节
mit6.8242 小时前
[nanoGPT] ChatGPT 的 LLM 的全栈实现 | 快速上手
python
DKunYu3 小时前
2.1线性回归
pytorch·python·深度学习·1024程序员节
大飞记Python3 小时前
实战分享:一键自动化下载指定版本的Chrome及Chromedriver(附Python源码)
chrome·python·自动化
程序员杰哥4 小时前
如何使用Postman做接口自动化测试及完美的可视化报告?
自动化测试·软件测试·python·测试工具·jenkins·postman·1024程序员节
老歌老听老掉牙4 小时前
参数曲线切向量与叉乘向量的精确计算与分析
python·sympy·1024程序员节
Cherry Zack5 小时前
FastAPI 入门指南 :基础概念与核心特性
开发语言·python·fastapi·1024程序员节
言德斐5 小时前
Python Web框架深度对比:Django vs Flask vs FastAPI(含优缺点与选型策略)
前端·python·django