计算题生成器(脱式计算,四~五年级(人教版)(无注释版))第一代

文章讲述了如何利用Python的Tkinter和random库编写一个生成四则运算题目的小程序,同时指出存在小bug,期待读者提出改进意见。
摘要由CSDN通过智能技术生成

废话不多说,上源码

from random import *
from tkinter import *

calculation_window = Tk()
calculation_window.title('计算题生成器')
Sx = 0

def CXSC():
    A_a = randint(10, 100)
    A_b = randint(10, 100)
    A_c = A_a
    A_d = 100-A_b
    A_e = str(A_a) + '×' + str(A_b) + '+' + str(A_c) + '×' + str(A_d)
    label1 = Label(calculation_window, text = A_e,font=('微软雅黑',15))
        
    B_a = round(uniform(10, 100), 2)
    B_b = round(uniform(10, 100), 2)
    B_c = B_a
    B_d = round(100-B_b, 2)
    B_e = str(B_a) + '×' + str(B_b) + '+' + str(B_c) + '×' + str(B_d)
    label2 = Label(calculation_window, text = B_e,font=('微软雅黑',15))
        
    C_a = round(uniform(10, 100), 2)
    C_b = round(uniform(10, 100), 2)
    C_c = C_a
    C_d = round(100-C_b, 2)
    C_e = str(C_a) + '×' + str(C_b) + '+' + str(C_c) + '×' + str(C_d)
    label3 = Label(calculation_window, text = C_e,font=('微软雅黑',15))
        
    D_a = round(uniform(10, 100), 2)
    D_b = round(uniform(10, 130), 2)
    D_c = D_a
    D_d = randint(100, 130) - round(D_b, 2)
    while D_d < 0:
        D_b = round(uniform(10, 130), 2)
        D_d = round(randint(100, 130) - round(D_b, 2))
    D_e = round(D_b + D_c - 100, 2)
    while D_e < 0:
        D_b = round(uniform(10, 130), 2)
        D_d = randint(100, 130) - round(D_b, 2)
        if D_d < 0:
            D_b = round(uniform(10, 130), 2)
            D_d = round(randint(100, 130) - round(D_b, 2))
        D_e = round(D_b + D_c - 100, 2)
    D_f = D_a    
    D_g = str(round(D_a, 2)) + '×' + str(round(D_b, 2)) + '+' + str(round(D_c, 2)) + '×' +str(round(D_d, 2)) + '-' + str(round(D_f, 2)) + '×' + str(round(D_e, 2))
    label4 = Label(calculation_window, text = D_g,font=('微软雅黑',15))
    label1.pack()
    label2.pack()
    label3.pack()
    label4.pack()
A_a = randint(10, 100)
A_b = randint(10, 100)
A_c = A_a
A_d = 100-A_b
A_e = str(A_a) + '×' + str(A_b) + '+' + str(A_c) + '×' + str(A_d)
label1 = Label(calculation_window, text = A_e,font=('微软雅黑',15))

B_a = round(uniform(10, 100), 2)
B_b = round(uniform(10, 100), 2)
B_c = B_a
B_d = round(100-B_b, 2)
B_e = str(B_a) + '×' + str(B_b) + '+' + str(B_c) + '×' + str(B_d)
label2 = Label(calculation_window, text = B_e,font=('微软雅黑',15))

C_a = round(uniform(10, 100), 2)
C_b = round(uniform(10, 100), 2)
C_c = C_a
C_d = round(100-C_b, 2)
C_e = str(C_a) + '×' + str(C_b) + '+' + str(C_c) + '×' + str(C_d)
label3 = Label(calculation_window, text = C_e,font=('微软雅黑',15))

D_a = round(uniform(10, 100), 2)
D_b = round(uniform(10, 130), 2)
D_c = D_a
D_d = randint(100, 130) - round(D_b, 2)
while D_d < 0:
    D_b = round(uniform(10, 130), 2)
    D_d = round(randint(100, 130) - round(D_b, 2))
D_e = round(D_b + D_c - 100, 2)
while D_e < 0:
    D_b = round(uniform(10, 130), 2)
    D_d = randint(100, 130) - round(D_b, 2)
    if D_d < 0:
        D_b = round(uniform(10, 130), 2)
        D_d = round(randint(100, 130) - round(D_b, 2))
    D_e = round(D_b + D_c - 100, 2)
D_f = D_a    
D_g = str(round(D_a, 2)) + '×' + str(round(D_b, 2)) + '+' + str(round(D_c, 2)) + '×' + str(round(D_d, 2)) + '-' + str(round(D_f, 2)) + '×' + str(round(D_e, 2))
label4 = Label(calculation_window, text = D_g,font=('微软雅黑',15))

CXSC_button = Button(calculation_window, text="继续生成",command=CXSC)

CXSC_button.pack()
label1.pack()
label2.pack()
label3.pack()
label4.pack()

calculation_window.mainloop()

这里用python的两个内置库:tkinter库 与 random库,代码比较简单,好理解。

还有点小bug,望哪位大神修改修改!
 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值