编辑代码

# coding:utf-8
#JSRUN引擎2.0,支持多达30种语言在线运行,全仿真在线交互输入输出。 
import pandas as pd
import random

def generate_carry_addition(num):
    questions = []
    while len(questions) < num:
        a = random.randint(1, 9)
        b = random.randint(max(10 - a, 1), 9)
        if a + b >= 10:
            question = f"{a} + {b} ="
            answer = a + b
            if (question, answer) not in questions:
                questions.append((question, answer))
    return questions

def generate_borrow_subtraction(num):
    questions = []
    while len(questions) < num:
        minuend = random.randint(11, 18)
        ones = minuend % 10
        subtrahend = random.randint(ones + 1, 9)
        question = f"{minuend} - {subtrahend} ="
        answer = minuend - subtrahend
        if (question, answer) not in questions:
            questions.append((question, answer))
    return questions

def generate_two_digit_add_tens(num):
    questions = []
    while len(questions) < num:
        two_digit = random.randint(10, 99)
        a = two_digit // 10
        max_c = 9 - a
        if max_c >= 1:
            c = random.randint(1, max_c)
            tens = c * 10
            question = f"{two_digit} + {tens} ="
            answer = two_digit + tens
            if (question, answer) not in questions:
                questions.append((question, answer))
    return questions

def generate_two_digit_subtract_tens(num):
    questions = []
    while len(questions) < num:
        tens = random.randint(1, 9) * 10
        two_digit = random.randint(tens, 99)
        question = f"{two_digit} - {tens} ="
        answer = two_digit - tens
        if (question, answer) not in questions:
            questions.append((question, answer))
    return questions

def generate_two_digit_add_ones(num):
    questions = []
    while len(questions) < num:
        two_digit = random.randint(10, 99)
        one = random.randint(0, 9)
        if two_digit + one <= 99:
            question = f"{two_digit} + {one} ="
            answer = two_digit + one
            if (question, answer) not in questions:
                questions.append((question, answer))
    return questions

def generate_two_digit_subtract_ones(num):
    questions = []
    while len(questions) < num:
        two_digit = random.randint(10, 99)
        one = random.randint(1, 9)
        question = f"{two_digit} - {one} ="
        answer = two_digit - one
        if (question, answer) not in questions:
            questions.append((question, answer))
    return questions

# 生成题目
carry_add = generate_carry_addition(30)        # 进位加法
borrow_sub = generate_borrow_subtraction(30)    # 退位减法
add_tens = generate_two_digit_add_tens(15)      # 加整十数
subtract_tens = generate_two_digit_subtract_tens(15)  # 减整十数
add_ones = generate_two_digit_add_ones(15)      # 加一位数
subtract_ones = generate_two_digit_subtract_ones(15)  # 减一位数

# 合并并打乱顺序
all_questions = carry_add + borrow_sub + add_tens + subtract_tens + add_ones + subtract_ones
random.shuffle(all_questions)

# 创建DataFrame并保存
df = pd.DataFrame(all_questions, columns=['题目', '答案'])
df.to_excel('计算题.xlsx', index=False, engine='openpyxl')

print("计算题已生成到 计算题.xlsx")