使用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}.")
相关推荐
栩栩云生28 分钟前
📥 x-cmd install | Toolong - 终端日志分析的瑞士军刀
运维·python·数据分析
前端小干将29 分钟前
安装python
python
星辰大海的精灵36 分钟前
Python 中利用算法优化性能的方法
后端·python
意.远37 分钟前
PyTorch实现权重衰退:从零实现与简洁实现
人工智能·pytorch·python·深度学习·神经网络·机器学习
会飞的土拨鼠呀39 分钟前
SP B\nRebuild Priorit> 如何用python去掉\n
开发语言·windows·python
David Bates1 小时前
代码随想录第18天:二叉树
python·算法·二叉树
TYUT_xiaoming1 小时前
python setup.py学习
python
A懿轩A2 小时前
2025年十六届蓝桥杯Python B组原题及代码解析
python·算法·蓝桥杯·idle·b组
程序媛徐师姐2 小时前
Python Django基于协同过滤算法的招聘信息推荐系统【附源码、文档说明】
python·django·协同过滤算法·招聘信息推荐系统·招聘信息·python招聘信息推荐系统·python招聘信息
2401_890665862 小时前
免费送源码:Java+ssm+MySQL 基于PHP在线考试系统的设计与实现 计算机毕业设计原创定制
java·hadoop·spring boot·python·mysql·spring cloud·php