python-小学四则运算出题

2021/9/26 20:40:47

本文主要是介绍python-小学四则运算出题,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

运行代码:

import tkinter as tk
import random

window = tk.Tk()
window.geometry('400x300')
window.title('四则运算练习')
window["bg"] = "pink"
ysfh = ['+','-','*','/']
# ss表示算式变量,jg表示结果变量,da表示答案变量,sr表示输入变量
ss = tk.StringVar()
ss.set('')
jg = tk.StringVar()
jg.set('')
da = tk.StringVar()
da.set('')
sr = tk.StringVar()
sr.set('')

tk.Label(window,text='算式:',justify=tk.RIGHT,width=80).place(x=15,y=60,width=80,height=20)
tk.Label(window,text='结果:',justify=tk.RIGHT,width=80).place(x=15,y=90,width=80,height=20)
tk.Label(window,text='答案:',justify=tk.RIGHT,width=80).place(x=15,y=120,width=80,height=20)

tk.Label(window,textvariable = ss).place(x=70,y=60)
tk.Label(window,textvariable = jg).place(x=70,y=90)
tk.Label(window,textvariable = da).place(x=70,y=120)

select = tk.IntVar() #Radiobutton单选
tk.Radiobutton(window,text='加法',variable = select,value=0).place(x=15,y=10)
tk.Radiobutton(window,text='减法',variable = select,value=1).place(x=85,y=10)
tk.Radiobutton(window,text='乘法',variable = select,value=2).place(x=155,y=10)
tk.Radiobutton(window,text='除法',variable = select,value=3).place(x=225,y=10)
tk.Radiobutton(window,text='混合运算',variable = select,value=4).place(x=295,y=10)

#评分
scores = tk.IntVar()
scores1 = tk.IntVar()
score = tk.IntVar()
scores.set(0)
scores1.set(0)
tk.Label(window,text='正确数:').place(x=250,y=60)
tk.Label(window,textvariable=scores).place(x=330,y=60)
tk.Label(window,text='错误数:').place(x=250,y=90)
tk.Label(window,textvariable=scores1).place(x=330,y=90)
tk.Label(window,text='得   分:').place(x=250,y=120)
tk.Label(window,textvariable=score).place(x=330,y=120)
#Entry对话框
En = tk.Entry(window,width=10,textvariable = sr)
En.place(x=150,y=60)

#检查答案
def check():
    b1.config(text='下一题')
    ss.set('')
    jg.set('')
    da.set('')
    sr.set('')
    ans = creat()
    def fun1_1(temp): #这里要接受来自bind()的变量
        sr_1 = En.get()
        if str(sr_1)==str(ans):
            jg.set('Correct')
            da.set(ans)
            temp = scores.get()
            a = score.get()
            temp +=1
            a +=1
            scores.set(temp)
            score.set(a)
        else:
            jg.set('Wrong')
            da.set(ans)
            temp1 = scores1.get()
            a = score.get()
            temp1 +=1
            a -=1
            scores1.set(temp1)
            score.set(a)
        with open('./Yunsuan.txt','a',encoding='utf-8') as fp:
            temp = '式子: '+ss.get()+sr.get()+'\n'
            fp.write(temp)
    En.bind('<Key-Return>',fun1_1) #Return后跳转到fun1_1

def exit_():
    window.destroy()

b1 = tk.Button(window,text='生成题目',command = check)
b1.place(x=100,y=200,width=50,height=20)
b2 = tk.Button(window,text='退出',command = exit_)
b2.place(x=250,y=200,width=50,height=20)

#生成题目
def creat():
    index = select.get()
    n1 = random.randint(1,100)
    n2 = random.randint(1,100)
    n3 = random.randint(1,100)
    result = 0
    if index!=4:   
        if index==0:
            result = n1+n2
        elif index == 1:
            n1,n2 = max(n1,n2),min(n1,n2)
            result = n1-n2
        elif index == 2:
            result = n1*n2
        elif index == 3:
            if n1 == n2:
                result = 1
            else:
                while n1%n2!=0:
                    n1 = random.randint(1,100)
                    n2 = random.randint(1,100)
                    n1,n2 = max(n1,n2),min(n1,n2)
                    result = int(n1/n2)
        temp = str(n1)+ysfh[index]+str(n2)+' ='
        ss.set(temp)
        return result
    elif index == 4:
        f1 = random.choice(ysfh)
        f2 = random.choice(ysfh)
        sz=str(n1)+f1+str(n2)+f2+str(n3)
        result = eval(sz)
        temp = str(n1)+f1+str(n2)+f2+str(n3)+' ='
        ss.set(temp)
        return result

window.mainloop()

运行结果:

 



这篇关于python-小学四则运算出题的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程