使用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}.")
相关推荐
一个闪现必杀技1 分钟前
Python入门--函数
开发语言·python·青少年编程·pycharm
小鹿( ﹡ˆoˆ﹡ )22 分钟前
探索IP协议的神秘面纱:Python中的网络通信
python·tcp/ip·php
卷心菜小温37 分钟前
【BUG】P-tuningv2微调ChatGLM2-6B时所踩的坑
python·深度学习·语言模型·nlp·bug
陈苏同学1 小时前
4. 将pycharm本地项目同步到(Linux)服务器上——深度学习·科研实践·从0到1
linux·服务器·ide·人工智能·python·深度学习·pycharm
唐家小妹1 小时前
介绍一款开源的 Modern GUI PySide6 / PyQt6的使用
python·pyqt
羊小猪~~2 小时前
深度学习项目----用LSTM模型预测股价(包含LSTM网络简介,代码数据均可下载)
pytorch·python·rnn·深度学习·机器学习·数据分析·lstm
Marst Code2 小时前
(Django)初步使用
后端·python·django
985小水博一枚呀2 小时前
【对于Python爬虫的理解】数据挖掘、信息聚合、价格监控、新闻爬取等,附代码。
爬虫·python·深度学习·数据挖掘
立秋67892 小时前
Python的defaultdict详解
服务器·windows·python
萧鼎3 小时前
Python第三方库选择与使用陷阱避免
开发语言·python