import random
defgenerate_vertical_division_problems(num_problems):
problems_with_answers =[]whilelen(problems_with_answers)< num_problems:# Generate random numbers for the problem
dividend = random.randint(10,99)# random two-digit number
divisor = random.randint(2,9)# random single-digit number# Calculate quotient and remainder
quotient = dividend // divisor
remainder = dividend % divisor
# Check if remainder is zero; if so, skip this iterationif remainder ==0:continue# Format the problem
problem =f" {len(problems_with_answers)+1}: {dividend} ÷ {divisor} ="# Store problem and answer separately
problems_with_answers.append((problem,(quotient, remainder)))return problems_with_answers
defsave_problems_and_answers(problems_with_answers, file_name, overwrite=False):
mode ='w'if overwrite else'a'withopen(file_name, mode)as f:for problem,(quotient, remainder)in problems_with_answers:
f.write(f"{problem}\n")if overwrite:
f.write("\nAnswers:\n\n")for index,(_,(quotient, remainder))inenumerate(problems_with_answers, start=1):
answer =f" {index}: {quotient}......{remainder}\n"
f.write(answer)# Generate 1000 vertical division problems
num_problems =1000
problems_with_answers = generate_vertical_division_problems(num_problems)# Save problems and answers to a single file, overwriting any existing file
file_name ='division_problems_and_answers.txt'
save_problems_and_answers(problems_with_answers, file_name, overwrite=True)print(f"Division problems and answers saved to '{file_name}'.")
选三位数进位加退位减各10道列竖式计算
import random
defgenerate_addition_problems(num_problems):
problems_with_answers =[]whilelen(problems_with_answers)< num_problems:# Generate random numbers for the problem
num1 = random.randint(100,999)# random three-digit number
num2 = random.randint(10,99)# random two-digit number# Calculate the problem and answer
problem =f"{num1}\n+ {num2}\n---"
answer = num1 + num2
# Store problem and answer together
problem_with_answer =(problem, answer)# Append to list of problems with answers
problems_with_answers.append(problem_with_answer)return problems_with_answers
defgenerate_subtraction_problems(num_problems):
problems_with_answers =[]whilelen(problems_with_answers)< num_problems:# Generate random numbers for the problem
num1 = random.randint(100,999)# random three-digit number
num2 = random.randint(10,99)# random two-digit number# Ensure num1 is greater than num2 for valid subtractionif num1 < num2:
num1, num2 = num2, num1
# Calculate the problem and answer
problem =f"{num1}\n- {num2}\n---"
answer = num1 - num2
# Store problem and answer together
problem_with_answer =(problem, answer)# Append to list of problems with answers
problems_with_answers.append(problem_with_answer)return problems_with_answers
defsave_problems_and_answers(problems_with_answers, file_name, overwrite=False):
problems_only =[]
answers_only =[]withopen(file_name,'w')as f:for index,(problem, answer)inenumerate(problems_with_answers, start=1):# Store problem in list
problems_only.append(f"题目 {index}:\n{problem}\n\n\n\n")# Store answer in list
answers_only.append(f"Answer {index}: {answer}\n")# Write all problems to file
f.write("Problems:\n\n")for problem in problems_only:
f.write(problem)import random
defgenerate_addition_problems(num_problems):
problems_with_answers =[]for _ inrange(num_problems):# Generate random numbers for the problem
num1 = random.randint(100,999)# random three-digit number
num2 = random.randint(10,99)# random two-digit number# Calculate the problem and answer
problem =f"{num1}+ {num2}=\n---"
answer = num1 + num2
# Store problem and answer together
problem_with_answer =(problem, answer)# Append to list of problems with answers
problems_with_answers.append(problem_with_answer)return problems_with_answers
defgenerate_subtraction_problems(num_problems):
problems_with_answers =[]for _ inrange(num_problems):# Generate random numbers for the problem
num1 = random.randint(100,999)# random three-digit number
num2 = random.randint(10,99)# random two-digit number# Ensure num1 is greater than num2 for valid subtractionif num1 < num2:
num1, num2 = num2, num1
# Calculate the problem and answer
problem =f"{num1}- {num2}= \n---"
answer = num1 - num2
# Store problem and answer together
problem_with_answer =(problem, answer)# Append to list of problems with answers
problems_with_answers.append(problem_with_answer)return problems_with_answers
defsave_problems_and_answers(problems_with_answers, file_name, overwrite=False):
problems_only =[]
answers_only =[]withopen(file_name,'w')as f:for index,(problem, answer)inenumerate(problems_with_answers, start=1):# Store problem in list
problems_only.append(f"题目 {index}:\n{problem}\n\n")# Store answer in list
answers_only.append(f"Answer {index}: {answer}\n")# Write all problems to file
f.write("Problems:\n\n")for problem in problems_only:
f.write(problem)# Write all answers to file
f.write("\n\nAnswers:\n\n")for answer in answers_only:
f.write(answer)# Generate 10 addition problems and 10 subtraction problems as one group
num_problems_per_type =1000
addition_problems = generate_addition_problems(num_problems_per_type)
subtraction_problems = generate_subtraction_problems(num_problems_per_type)# Save problems and answers to a single file, overwriting any existing file
file_name ='选三位数进位加退位减各10道列竖式计算.txt'
save_problems_and_answers(addition_problems + subtraction_problems, file_name, overwrite=True)print(f"Addition and subtraction problems saved to '{file_name}'.")# Write all answers to file
f.write("\n\nAnswers:\n\n")for answer in answers_only:
f.write(answer)# Generate 10 addition problems and 10 subtraction problems
num_problems =1000
addition_problems = generate_addition_problems(num_problems)
subtraction_problems = generate_subtraction_problems(num_problems)# Save problems and answers to a single file, overwriting any existing file
file_name ='选三位数进位加退位减各10道列竖式计算.txt'
save_problems_and_answers(addition_problems + subtraction_problems, file_name, overwrite=True)print(f"Addition and subtraction problems saved to '{file_name}'.")