python代码:
import random
import os
def generate_expression(operators='+', digit_length=2):
operator = random.choice(operators)
num1 = random.randint(10**(digit_length-1), 10**digit_length - 1)
num2 = random.randint(10**(digit_length-1), 10**digit_length - 1)
if operator == '+':
answer = num1 + num2
elif operator == '-':
num1, num2 = max(num1, num2), min(num1, num2) # Ensure result is non-negative
answer = num1 - num2
elif operator == '*':
answer = num1 * num2
elif operator == '/':
num2 = random.randint(1, 9) # Ensure num2 is not zero
num1, num2 = num1 * num2, num2 # Ensure result is an integer
answer = num1 // num2
expression = f"{num1} {operator} {num2}"
return expression, answer
def generate_questions(num_questions, num_add, num_subtract, num_multiply, num_divide, digit_length=2):
questions = []
for _ in range(num_add):
questions.append(generate_expression('+', digit_length))
for _ in range(num_subtract):
questions.append(generate_expression('-', digit_length))
for _ in range(num_multiply):
questions.append(generate_expression('*', digit_length))
for _ in range(num_divide):
questions.append(generate_expression('/', digit_length))
random.shuffle(questions)
return questions
def save_to_file(questions, filename):
with open(filename, 'w') as f:
for i, (question, answer) in enumerate(questions, 1):
f.write(f"{i}. {question}\n")
with open(f'answers_{filename}', 'w') as f:
for i, (_, answer) in enumerate(questions, 1):
f.write(f"{i}. {answer}\n")
def main():
num_questions = int(input("请输入题目数量:"))
num_add = int(input("请输入加法题数量:"))
num_subtract = int(input("请输入减法题数量:"))
num_multiply = int(input("请输入乘法题数量:"))
num_divide = int(input("请输入除法题数量:"))
digit_length = int(input("请输入数字位数:"))
questions = generate_questions(num_questions, num_add, num_subtract, num_multiply, num_divide, digit_length)
save_to_file(questions, 'questions.txt')
print("口算题已保存到 questions.txt 文件中")
print("答案已保存到 answers_questions.txt 文件中")
if __name__ == "__main__":
main()
【 在 jdjr 的大作中提到: 】
: 小学5年级,大佬们有推荐的能出题的APP吗,要求能生成word或者pdf能打印的
--
修改:USSRCCCP FROM 222.129.2.*
FROM 222.129.2.*