小学生加减练习题库生成器
本帖最后由 longhua23 于 2024-6-28 09:45 编辑import random
from openpyxl import Workbook
from openpyxl.utils import get_column_letter
import tkinter as tk
from tkinter import messagebox
from tkinter import ttk
import os,sys
# 生成加法运算题目
def generate_addition_problems(count=10, s=10, z=2):
problems = []
for _ in range(count):
if z == 2:
a = random.randint(1, s)
b = random.randint(1, s)
answer=a+b
problems.append((f"{a} + {b} = ",answer))
elif z == 3:
a = random.randint(1, s)
b = random.randint(1, s)
c = random.randint(1, s)
answer=a+b+c
problems.append((f"{a} + {b} + {c} = ",answer))
return problems
# 生成减法运算题目
def generate_subtraction_problems(count=10, s=10, z=2):
problems = []
for _ in range(count):
if z == 2:
a = random.randint(1, s)
b = random.randint(1, a)# 确保减法不出现负数
answer=a-b
problems.append((f"{a} - {b} = ",answer))
elif z == 3:
a = random.randint(1, s)
b = random.randint(1, a)
c = random.randint(1, s)
if b + c > a:
b, c = c, b
answer=a-b-c
problems.append((f"{a} - {b} - {c} = ",answer))
return problems
# 生成加减混合运算题目
def generate_mixed_problems(count=10, s=10, z=2):
problems = []
for _ in range(count):
if z == 2:
a = random.randint(1, s)
b = random.randint(1, s)
if random.choice(['+', '-']) == '+':
answer = a + b
problems.append((f"{a} + {b} = ", answer))
else:
if a < b:
a, b = b, a# 确保减法不出现负数
answer = a - b
problems.append((f"{a} - {b} = ", answer))
elif z == 3:
a = random.randint(1, s)
b = random.randint(1, s)
c = random.randint(1, s)
operators = random.choices(['+', '-'], k=2)
if operators == '-' and b > a:
a, b = b, a
if operators == '-' and c > (a - b if operators == '-' else a + b):
b, c = c, b
if operators == '+':
temp_result = a + b
else:
temp_result = a - b
if operators == '+':
answer = temp_result + c
else:
answer = temp_result - c
problem = f'{a}{operators} {b} {operators} {c} ='
problems.append((problem, answer))
return problems
# 将题目写入sheet表
def write_problems_to_sheet(sheet, problems,answers=False):
row = 1
col = 1
for i, (problem,answer)in enumerate(problems, start=1):
# 写入题目编号
sheet = f"{i}."
# 写入题目和分隔线
sheet = problem
if answers==False:
sheet = "( )"
else:
sheet = f"( {answer} )"
row += 1
# 每50行换一列
if row > 50:
row = 1
col += 3# 每题占用3列
# 创建Excel文件并添加三个sheet表
def create_excel_file(filename, addition_problems, subtraction_problems, mixed_problems,answers):
wb = Workbook()
# 加法运算
ws1 = wb.active
ws1.title = "加法运算"
write_problems_to_sheet(ws1, addition_problems,answers)
# 减法运算
ws2 = wb.create_sheet(title="减法运算")
write_problems_to_sheet(ws2, subtraction_problems,answers)
# 加减混合运算
ws3 = wb.create_sheet(title="加减混合运算")
write_problems_to_sheet(ws3, mixed_problems,answers)
# 保存文件
wb.save(filename)
# 主函数
def main():
def entry():
try:
s = int(entry_max_value.get())
x = int(entry_num_problems.get())
z = int(entry_num_operands.get())
addition_problems = generate_addition_problems(x, s, z)
subtraction_problems = generate_subtraction_problems(x, s, z)
mixed_problems = generate_mixed_problems(x, s, z)
current_dir=os.path.dirname(sys.executable)
print(current_dir)
filename_problems=os.path.join(current_dir,'题目.xlsx')
filename_answers=os.path.join(current_dir,'答案.xlsx')
create_excel_file(filename_problems, addition_problems, subtraction_problems, mixed_problems,answers=False)
create_excel_file(filename_answers, addition_problems, subtraction_problems, mixed_problems,answers=True)
output_text.insert(tk.END,f"题目文件与答案文件已生成,保存位置\n{current_dir}")
except Exception as e:
output_text.insert(tk.END,f'发生错误{str(e)}\n')
print(e)
finally:
output_text.see(tk.END)
# 创建主窗口
root = tk.Tk()
root.title('运算题目生成器')
dir=os.path.dirname(os.path.abspath(__file__))
ico_path=os.path.join(dir,'jisuan2.ico')
root.iconbitmap(ico_path)
root.geometry("550x450")
# 设置主窗口背景颜色为白色
root.config(bg='white')
font = ("Arial", 14)
# 输入框
tk.Label(root, text='输入题目最大值:',font=font,bg='white').grid(row=0, column=0,padx=10,pady=20,sticky='e')
entry_max_value = tk.Entry(root,font=font,width=10,bd=2,bg='lightyellow',fg='blue',relief='sunken')
entry_max_value.grid(row=0, column=1,padx=10,pady=20)
tk.Label(root, text='输入生成的题数:',font=font,bg='white').grid(row=1, column=0,padx=10,pady=20,sticky='e')
entry_num_problems = tk.Entry(root,font=font,width=10,bd=2,bg='lightyellow',fg='blue',relief='sunken')
entry_num_problems.grid(row=1, column=1,padx=10,pady=20)
tk.Label(root, text='可选择几则运算:',font=font,bg='white').grid(row=2, column=0,padx=10,pady=20,sticky='e')
entry_num_operands=ttk.Combobox(root,values=,font=font,width=8,style='TCombobox')
entry_num_operands.grid(row=2, column=1,padx=10,pady=20)
entry_num_operands.current(0)
# 按钮
button = tk.Button(root, text= '''生成题目''',
command=entry,font=font,width=10,bd=5,fg='black',padx=10,pady=10,)
# button.place(x=350,y=60)
button.grid(row=3,columnspan=2,padx=0,pady=0)
#输出显示
output_text=tk.Text(root,font=font,height=5,width=35,bd=2,relief='sunken',bg='lightyellow')
output_text.grid(row=4,columnspan=2,padx=10,pady=20)
# 让所有列具有相同的扩展权重,从而在窗口大小调整时居中对齐
root.grid_columnconfigure(0, weight=1)
root.grid_columnconfigure(1, weight=1)
# 让所有行具有相同的扩展权重,从而在窗口大小调整时居中对齐
# root.grid_rowconfigure(0, weight=1)
# root.grid_rowconfigure(1, weight=1)
# root.grid_rowconfigure(2, weight=1)
# root.grid_rowconfigure(3, weight=1)
root.mainloop()
if __name__ == "__main__":
main()
马上暑假了,得给娃娃找点事情做。做了一个加减练习的题库。
已打包为成品,下载即可使用。
使用介绍:
1、可以自定义输入最大的数字,确保在想要的范围内
2、可以自定义生成题目的数量。
3、目前支持+-加减两则运算。可以选择2或3。2代表1+1 或1-1,,,3代表1+1+1或 1-1+1
4、结果会生成两份excle,为没答案版本和答案版本。方便计算不好的家长或者娃娃自己进行答案核对。
5、每份excle里面有3个工作簿。对应 单一加,单一减,有加有减。
6、做过多次测试了,打开excle文件,手动调整一下列宽,基本可以直接打印,选择最窄的页边距。一页A4纸 50行 4列。这样最省打印纸。
下载链接:
下载:https://wwp.lanzoue.com/iXawf22v5ikj 密码:7d3x
本帖最后由 longhua23 于 2024-7-8 17:54 编辑
https://www.52pojie.cn/forum.php?mod=viewthread&tid=1942067&page=1#pid50750796
更新第二版,在软件区了,有兴趣的伙伴可以前去。后面这个主题都会在软件区进行更新。 非常好用,娃的数学辅导班钱省了
import random
from openpyxl import Workbook
from openpyxl.utils import get_column_letter
import tkinter as tk
from tkinter import messagebox
from tkinter import ttk
import os, sys
# 生成加法运算题目
def generate_addition_problems(count=10, s=10, z=2):
problems = []
for _ in range(count):
if z == 2:
a = random.randint(1, s)
b = random.randint(1, s)
answer = a + b
problems.append((f"{a} + {b} = ", answer))
elif z == 3:
a = random.randint(1, s)
b = random.randint(1, s)
c = random.randint(1, s)
answer = a + b + c
problems.append((f"{a} + {b} + {c} = ", answer))
return problems
# 生成减法运算题目
def generate_subtraction_problems(count=10, s=10, z=2):
problems = []
for _ in range(count):
if z == 2:
a = random.randint(1, s)
b = random.randint(1, a)# 确保减法不出现负数
answer = a - b
problems.append((f"{a} - {b} = ", answer))
elif z == 3:
a = random.randint(1, s)
b = random.randint(1, a)
c = random.randint(1, s)
if b + c > a:
b, c = c, b
answer = a - b - c
problems.append((f"{a} - {b} - {c} = ", answer))
return problems
# 生成加减混合运算题目
def generate_mixed_problems(count=10, s=10, z=2):
problems = []
for _ in range(count):
if z == 2:
a = random.randint(1, s)
b = random.randint(1, s)
if random.choice(['+', '-']) == '+':
answer = a + b
problems.append((f"{a} + {b} = ", answer))
else:
if a < b:
a, b = b, a# 确保减法不出现负数
answer = a - b
problems.append((f"{a} - {b} = ", answer))
elif z == 3:
a = random.randint(1, s)
b = random.randint(1, s)
c = random.randint(1, s)
operators = random.choices(['+', '-'], k=2)
if operators == '-' and b > a:
a, b = b, a
if operators == '-' and c > (a - b if operators == '-' else a + b):
b, c = c, b
if operators == '+':
temp_result = a + b
else:
temp_result = a - b
if operators == '+':
answer = temp_result + c
else:
answer = temp_result - c
problem = f'{a}{operators} {b} {operators} {c} ='
problems.append((problem, answer))
return problems
# 将题目写入sheet表
def write_problems_to_sheet(sheet, problems, answers=False):
row = 1
col = 1
for i, (problem, answer) in enumerate(problems, start=1):
# 写入题目编号
sheet = f"{i}."
# 写入题目和分隔线
sheet = problem
if not answers:
sheet = "( )"
else:
sheet = f"( {answer} )"
row += 1
# 每50行换一列
if row > 50:
row = 1
col += 3# 每题占用3列
# 创建Excel文件并添加三个sheet表
def create_excel_file(filename, addition_problems, subtraction_problems, mixed_problems, answers):
wb = Workbook()
# 加法运算
ws1 = wb.active
ws1.title = "加法运算"
write_problems_to_sheet(ws1, addition_problems, answers)
# 减法运算
ws2 = wb.create_sheet(title="减法运算")
write_problems_to_sheet(ws2, subtraction_problems, answers)
# 加减混合运算
ws3 = wb.create_sheet(title="加减混合运算")
write_problems_to_sheet(ws3, mixed_problems, answers)
# 保存文件
wb.save(filename)
# 主函数
def main():
def entry():
try:
s = int(entry_max_value.get())
x = int(entry_num_problems.get())
z = int(entry_num_operands.get())
addition_problems = generate_addition_problems(x, s, z)
subtraction_problems = generate_subtraction_problems(x, s, z)
mixed_problems = generate_mixed_problems(x, s, z)
current_dir = os.path.dirname(sys.executable)
filename_problems = os.path.join(current_dir, '题目.xlsx')
filename_answers = os.path.join(current_dir, '答案.xlsx')
create_excel_file(filename_problems, addition_problems, subtraction_problems, mixed_problems, answers=False)
create_excel_file(filename_answers, addition_problems, subtraction_problems, mixed_problems, answers=True)
output_text.insert(tk.END, f"题目文件与答案文件已生成,保存位置\n{current_dir}\n")
except Exception as e:
output_text.insert(tk.END, f'发生错误: {str(e)}\n')
finally:
output_text.see(tk.END)
# 创建主窗口
root = tk.Tk()
root.title('运算题目生成器')
ico_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'jisuan2.ico')
try:
root.iconbitmap(ico_path)
except Exception as e:
print(f"无法设置图标: {e}")
root.geometry("550x450")
root.config(bg='white')
font = ("Arial", 14)
# 输入框
tk.Label(root, text='输入题目最大值:', font=font, bg='white').grid(row=0, column=0, padx=10, pady=20, sticky='e')
entry_max_value = tk.Entry(root, font=font, width=10, bd=2, bg='lightyellow', fg='blue', relief='sunken')
entry_max_value.grid(row=0, column=1, padx=10, pady=20)
tk.Label(root, text='输入生成的题数:', font=font, bg='white').grid(row=1, column=0, padx=10, pady=20, sticky='e')
entry_num_problems = tk.Entry(root, font=font, width=10, bd=2, bg='lightyellow', fg='blue', relief='sunken')
entry_num_problems.grid(row=1, column=1, padx=10, pady=20)
tk.Label(root, text='可选择几则运算:', font=font, bg='white').grid(row=2, column=0, padx=10, pady=20, sticky='e')
entry_num_operands = ttk.Combobox(root, values=, font=font, width=8)
entry_num_operands.grid(row=2, column=1, padx=10, pady=20)
entry_num_operands.current(0)
# 按钮
button = tk.Button(root, text='生成题目', command=entry, font=font, width=10, bd=5, fg='black', padx=10, pady=10)
button.grid(row=3, columnspan=2, padx=0, pady=0)
# 输出显示
output_text = tk.Text(root, font=font, height=5, width=35, bd=2, relief='sunken', bg='lightyellow')
output_text.grid(row=4, columnspan=2, padx=10, pady=20)
root.grid_columnconfigure(0, weight=1)
root.grid_columnconfigure(1, weight=1)
root.mainloop()
if __name__ == "__main__":
main()
一、本版块仅限分享编程技术和源码相关内容,发布帖子必须带上关键代码和具体功能介绍【20220924强制执行】,详见『代码插入规范』。
按要求添加关键代码。 这是编程语言区,不是原创区和精品区,需要放代码的啊 亲情止于一年级上册有你一份功劳 Hmily 发表于 2024-6-27 18:20
按要求添加关键代码。
可能操作错误了,没加上去。重新发了 wyl0205 发表于 2024-6-27 20:14
这是编程语言区,不是原创区和精品区,需要放代码的啊
可能操作有错误,重新上传了 zjtzjt 发表于 2024-6-27 20:55
感谢分享,娃娃笑哈哈
鸡娃必须从小开始培养{:17_1067:} gxsnowing 发表于 2024-6-28 09:15
亲情止于一年级上册有你一份功劳
有空了在搞点,乘除 小数点和负数,。让整个小学都不存在亲情 默默点赞 谢谢分享哦