How to reduce the instruction block inserted within a conditional if structure in python?

Asked

Viewed 118 times

1

Hi, I’m developing a program that calculates and sizes wall beams. It works basically with security checks (request less or equal to resistance), when one of these checks is not met, the program will issue an alert to the user informing the error and then will propose a solution. But the block of instructions contained inside this if became very large and consequently has been causing some errors. I wonder how I can narrow down lines of that code. (Note: all this code is contained in the confirm function, the website editor did not represent the same way I copied)

def confirmar():
lff = float(raid1.get()) * 0.01
h = float(raid2.get()) * 0.01
bw = float(raid3.get()) * 0.01
c1 = float(raid4.get()) * 0.01
c2 = float(raid5.get()) * 0.01
fck = float(raid6.get())
Gi = float(raid7.get())
Gs = float(raid8.get())
hf = float(raid9.get()) * 0.01
c = float(raid10.get()) * 0.01
c3 = float(raid13.get()) * 0.01

# Comprimento total
ltot = (lff + c1 + c2)

# Vão de eixo a eixo
lee = float(lff + c1 / 2 + c2 / 2)

# Relação vão altura
vh = (lee / h)

# Tipo de Viga
if vh >= 2:
    erro = tkinter.messagebox.showerror("Erro na verificação",
                                        "A Estrutura apresentada não se classifica como viga-parede")
    verificacao = tkinter.messagebox.askyesno("XimeS - Cálculo de viga-parede", "Deseja informar novos dados?")
    if verificacao > 0:
        limpar()
    elif verificacao == 0:
        janela_principal.destroy()

# Vão teórico
le = min(lee, 1.15 * lff)
# print(f'#Vão teórico: \033[33m{le:.2f}m\033[m')

# Altura efetiva
he = min(le, h)
# print(f'#Altura efetiva: \033[33m{he:.2f}m\033[m')

# Carregamento
Q = (bw * h * 25) + Gi + Gs
# print(f'#Carregamento: \033[33m{Q:.2f} KN/m\033[m')

# Esforços Solicitantes
V = (Q * le) / 2
M = (Q * pow(le, 2)) / 8
# print(f'#Força Solicitante: \033[33m{V:.2f} KN\033[m')
# print(f'#Momento Máximo: \033[33m{M:.2f} KN.m\033[m')

# Verificação do Concreto
# Cisalhamento
fcd = (fck * 1000) / 1.4
Vrd = 0.20 * bw * he * fcd
Vd = 1.4 * V

# print(f'#Solicitação de Cálculo: \033[33m{Vd:.2f} KN\033[m')
# print(f'#Resistência ao Cisalhamento: \033[33m{Vrd:.2f} KN\033[m')
def bt_click_nao1():
    verificacao1.destroy()
    return

def bt_click_sim1():
    bt_sim1["state"] = "disabled"
    global raid11
    raid11 = IntVar()

    def ck1():
        ck_Afck["state"] = "disabled"
        en_fck["state"] = "disabled"
        global teste
        teste = 1
        return

    def ck2():
        ck_Alargura["state"] = "disabled"
        en_largura["state"] = "disabled"
        global teste
        teste = 2
        return

    def reset():
        ck_Alargura["state"] = "normal"
        ck_Afck["state"] = "normal"
        ck_Alargura.deselect()
        ck_Afck.deselect()
        en_largura["state"] = "normal"
        en_fck["state"] = "normal"
        en_largura.delete(first=0, last=11)
        en_fck.delete(first=0, last=11)
        return

    def confirmar1():
        verificacao1.destroy()

        if teste == 1:
            bw1 = raid11.get() * 0.01
            # Carregamento
            Q = (bw1 * h * 25) + Gi + Gs
            # print(f'#Novo Carregamento: \033[33m{Q:.2f} KN/m\033[m')

            # Esforços Solicitantes
            V = (Q * le) / 2
            M = (Q * pow(le, 2)) / 8
            # print(f'#Nova Cortante: \033[33m{V:.2f} KN\033[m')
            # print(f'#Novo Momento Máximo: \033[33m{M:.2f} KN.m\033[m')

            # Verificação do Concreto
            # Cisalhamento
            fcd = (fck * 1000) / 1.4
            Vrd = 0.20 * bw1 * he * fcd
            Vd = 1.4 * V
            print(f'#Nova cortante de cálculo: \033[33m{Vd:.2f} KN\033[m')
            print(f'#Nova resistênca de cálculo: \033[33m{Vrd:.2f} KN\033[m')

        if teste == 2:
            # Carregamento

            Q = (bw * h * 25) + Gi + Gs
            # print(f'#Novo Carregamento: \033[33m{Q:.2f} KN/m\033[m')

            # Esforços Solicitantes
            V = (Q * le) / 2
            M = (Q * pow(le, 2)) / 8
            # print(f'#Nova Cortante: \033[33m{V:.2f} KN\033[m')
            # print(f'#Novo Momento Máximo: \033[33m{M:.2f} KN.m\033[m')

            # Verificação do Concreto
            # Cisalhamento
            fcd = (fck * 1000) / 1.4
            vrd = 0.20 * bw * he * fcd
            Vd = 1.4 * V
            # print(f'#Nova cortante de cálculo: \033[33m{Vd:.2f} KN\033[m')
            # print(f'#Nova resistênca de cálculo: \033[33m{Vrd:.2f} KN\033[m')

            # Zona de Apoio
            Vd1 = Vd * 1.1
            Vrd1 = 0.8 * bw * (c1 + hf) * fcd
            print(f'Solicitação na zona de apoio: \033[33m{Vd1:.2f} KN\033[m')
            print(f'Resistência da zona de apoio: \033[33m{Vrd1:.2f} KN\033[m')
        return

    # textos e CheckButtons de escolha
    lb_escolaSolucao = Label(frame_sugestoes, text="ESCOLHA A SOLUÇÃO:", font=("Arial", 12, "bold"), pady=10,
                             bd=3, fg="#1E90FF", anchor="center")
    lb_escolaSolucao.grid(row=0, column=3)
    ck_Alargura = Checkbutton(frame_sugestoes, text="Aumentar largura", font=("Segoe UI", 12), command=ck1)
    ck_Alargura.grid(row=1, column=3, sticky=W)
    ck_Afck = Checkbutton(frame_sugestoes, text="Aumentar o Fck", font=("Segoe UI", 12), command=ck2)
    ck_Afck.grid(row=2, column=3, sticky=W)

    # Entrada de dados
    en_largura = Entry(frame_sugestoes, font=("Ce/ntury Gothic", 11), textvariable=raid11, bd=5, insertwidth=4,
                       width=12, bg="#1E90FF", justify="right")
    en_largura.grid(row=1, column=4)
    en_fck = Entry(frame_sugestoes, font=("Century Gothic", 11), textvariable=raid12, bd=5, insertwidth=4,
                   width=12, bg="#1E90FF", justify="right")
    en_fck.grid(row=2, column=4)

    # botões reset e confirmar
    bt_reset = Button(frame_sugestoes, text="RESET", font=("Arial", 12, "bold"), bg="#1E90FF", padx=16, pady=0,
                      bd=2, width=7, command=reset)
    bt_reset.grid(row=4, column=3, sticky=E)
    bt_confirmar1 = Button(frame_sugestoes, text="CONFIRMAR", font=("Arial", 12, "bold"), bg="#1E90FF", padx=16,
                           pady=0, bd=2, width=7, command=confirmar1)
    bt_confirmar1.grid(row=4, column=4, sticky=W)

if Vrd < Vd:
    bws = str("Utilize uma largura mínima de viga de %.f cm" % ((Vd / (0.2 * he * fcd)) * 100))
    fcks = str("Utilize um Fck mínimo de %.f MPa" % (1.4 * Vd / (200 * bw * he)))

    # Janela de verificação de cisalhamento
    verificacao1 = Tk()
    verificacao1.title("ERRO NA VERIFICAÇÃO")
    verificacao1.geometry("+300+100")

    # Frames
    frame_top1 = Frame(verificacao1)
    frame_top1.pack()
    frame_sugestoes = Frame(verificacao1)
    frame_sugestoes.pack(side=LEFT)

    # Textos
    lb_alerta1 = Label(frame_top1, text="A ESTRUTURA NÃO SUPORTA O EFEITO DE CISALHAMENTO NO CONCRETO",
                       font=("Arial", 13, "bold"), pady=12, fg="#1E90FF")
    lb_alerta1.grid(row=0, column=0)
    lb_psugestoes = Label(frame_sugestoes, text="POSSÍVEIS SOLUÇÕES", font=("Arial", 12, "bold"), pady=10, bd=3,
                          fg="#1E90FF")
    lb_psugestoes.grid(row=0, column=0)
    lb_largura = Label(frame_sugestoes, text=bws, font=("Segoe UI", 12))
    lb_largura.grid(row=1, column=0, sticky=W)
    lb_fck = Label(frame_sugestoes, text=fcks, font=("Segoe UI", 12))
    lb_fck.grid(row=2, column=0, sticky=W)
    lb_quest1 = Label(frame_sugestoes, text="Deseja aplicar alguma solução? ", font=("Segoe UI", 12))
    lb_quest1.grid(row=4, column=0, sticky=W)
    lb_linha1 = Label(frame_sugestoes)
    lb_linha1.grid(row=3, column=0)

    # botões - SIM/NÃO
    bt_sim1 = Button(frame_sugestoes, text="SIM", font=("Arial", 12, "bold"), bg="#1E90FF", padx=8, pady=0, bd=2,
                     width=5, command=bt_click_sim1)
    bt_sim1.grid(row=4, column=0, sticky=E)
    bt_nao1 = Button(frame_sugestoes, text="NÃO", font=("Arial", 12, "bold"), bg="#1E90FF", padx=8, pady=0, bd=2,
                     width=5, command=bt_click_nao1)
    bt_nao1.grid(row=4, column=2, sticky=W)
    verificacao1.mainloop()
  • In order to be able to better answer this question, we need to know "what has outside of if". What code arrow the variables Vrd and Vd?

  • right.. @jsbueno all this if is contained in a function that I called confirm is assigned to a button also called CONFIRM. I will edit the question and enter the code above the if.

  • What kind of errors would be generating? Related to logic, design or structuring?

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.