Python编写的小学加减乘除算术练习题源码
以下是一个简单的Python源码,用于生成小学加减乘除算术练习题: ```python import random def generate_question(): num1 = random.randint(1, 10) num2 = random.randint(1, 10) operator = random.choice(["+", "-", "*", "/"]) if operator == "+": question = f"{num1} + {num2}" answer = num1 + num2 elif operator == "-": question = f"{num1} - {num2}" answer = num1 - num2 elif operator == "*": question = f"{num1} * {num2}" answer = num1 * num2 else: if num2 != 0: question = f"{num1
马上放暑假了,用python开发了一个,小学加减乘除算术练习题自动出题工具,可以自动出题,自动判断对错。
fromtkinterimport*importrandomdefjiajian():#加减法函数,生成加数,和,被减数,减数,差joj=random.randint(1,2)a=random.randint(1,999)b=random.randint(1,999)ifjoj==1:#随机产生h=a+breturn[a,'+',b,h]else:ifa>b:h=a-breturn[a,'-',b,h]else:h=b-areturn[b,'-',a,h]defcc(i=1):#乘除运算,返回乘数,积。i表示难度,1为个位数,2为2位数,3为3位数,j,1是乘,2是除jc=random.randint(1,2)ifi==1:#难度1a=random.randint(2,9)b=random.randint(2,9)j=a*bifjc==1:#乘法return[a,'*',b,j]#1为乘法,2为除法else:return[j,'/',a,b]#1为乘法,2为除法elifi==2:#难度2a=random.randint(11,99)b=random.randint(11,99)j=a*bifjc==1:#乘法return[a,'*',b,j]#1为乘法,2为除法else:return[j,'/',a,b]#1为乘法,2为除法else:#难度3a=random.randint(101,999)b=random.randint(101,999)j=a*bifjc==1:#乘法return[a,'*',b,j]#1为乘法,2为除法else:return[j,'/',a,b]#1为乘法,2为除法defyz():#这是按钮的验证函数。globallistaifent1.get()==str(lista[3]):ndsel()lab1.config(text=str(lista[0])+lista[1]+str(lista[2])+'=')lab2.config(text='上一题解题正确,已更新题目')lab2.config(fg='green')ent1.delete(0,END)ent1.focus()else:#回答错误,清除文本框,标签2更新ent1.delete(0,END)ent1.focus()lab2.config(text='解答错误请重新输入')lab2.config(fg='red')defndsel():#单选按钮难度设定globallistaa=nd.get()ifa==1:lista=jiajian()lab1.config(text=str(lista[0])+lista[1]+str(lista[2])+'=')lab2.config(text='题目已变更')lab2.config(fg='green')ent1.delete(0,END)ent1.focus()else:lista=cc(a-1)lab1.config(text=str(lista[0])+lista[1]+str(lista[2])+'=')lab2.config(text='题目已变更')lab2.config(fg='green')ent1.delete(0,END)ent1.focus()lista=jiajian()root=Tk()root.title("小学加减乘除练习")root.geometry('600x400')labf1=LabelFrame(root,text='模式选择',width=500,height=150)labf1.pack()nd=IntVar()nd.set(1)rb1=Radiobutton(labf1,text='加减法运算',variable=nd,value=1,command=ndsel)rb2=Radiobutton(labf1,text='十以内乘除法',variable=nd,value=2,command=ndsel)rb3=Radiobutton(labf1,text='百以内乘除法',variable=nd,value=3,command=ndsel)rb4=Radiobutton(labf1,text='千以内乘除法',variable=nd,value=4,command=ndsel)rb1.pack(side=LEFT)rb2.pack(side=LEFT)rb3.pack(side=LEFT)rb4.pack(side=LEFT)lab1=Label(root,text=str(lista[0])+lista[1]+str(lista[2])+'=',font=('宋体',28))lab1.pack()ent1=Entry(root,font=('宋体',28),width=10)ent1.pack()but1=Button(root,text='确定',font=('宋体',28),command=yz)but1.pack()lab2=Label(root,text='请在文本框内输入正确答案,点击确定键验算',pady=5)lab2.pack()'''f=open('a.txt','w',encoding='utf8')foriinrange(10000):listb=jiajian()f.write(str(listb[0])+listb[1]+str(listb[2])+'='+"\t\t\t")f.close()'''root.mainloop()