嵌套的 IF-ELIF 语句

IF-ELIF statements nested

我想知道是否有更好的写法。下面所述的当前代码有效。我只是想知道是否有更好的方法。

这是为了验证。所以 if 语句检查所有必填字段,如果它们是可接受的,那么它会进入执行代码。问题开始是因为

的输入字段

self.text_fmax

未被接受为整数。所以这个特定的字段,首先检查它是否为空然后忽略,如果不是那么该值必须是 0 到 180 之间的整数。

  def call_back(self):
    if len(self.text_n.get()) == 0:
        tkMessageBox.showinfo("Mandatory Information", "Please input an integer value for Number of Tessellations Cells")
    elif len(self.text_id.get()) == 0:
        tkMessageBox.showinfo("Mandatory Information", "Please input an integer value for Tessellation Identifier")
    elif len(domain_container) == 0:
        tkMessageBox.showinfo("Mandatory Information", "Please input Domain")
    elif len(self.text_fmax.get()) != 0:
        a = int(self.text_fmax.get())
        if a < 0 or a > 180:
            tkMessageBox.showinfo("Incorrect Value", "Face Flatness should be less than 180")
        elif len(filename4) == 0:
            tkMessageBox.showinfo("Mandatory Information", "Please input Output File Name")
        else:
            self.execute_neper_code()
    elif len(filename4) == 0:
        tkMessageBox.showinfo("Mandatory Information", "Please input Output File Name")
    else:
        self.execute_neper_code()

您通常不需要检查len == 0,只需测试对象"truthiness"。

def call_back(self):
    show = tkMessageBox.showinfo
    if not self.text_n.get():
        show("Mandatory Information", "Please input an integer value for Number of Tessellations Cells")

    elif not self.text_id.get():
        show("Mandatory Information", "Please input an integer value for Tessellation Identifier")

    elif not domain_container:
        show("Mandatory Information", "Please input Domain")

    elif self.text_fmax.get() and not 0 <= int(self.text_fmax.get()) <= 180:
        show("Incorrect Value", "Face Flatness should be less than 180")

    elif not filename4:
        show("Mandatory Information", "Please input Output File Name")

    else:
        self.execute_neper_code()

如果你想避免重复的代码和字符串,你可以试试下面的方法。 以下不是完整的代码,而是简单的示例。 t_msgs dict 中的键表示要验证的字段和要验证的值(可以是范围)。

def call_back(self):
    t_msgs = {"mdt": "Mandatory Information",
              "incrt_val" :"Incorrect Value"}
    checkFields = {(self.text_n, 0): 
                    (t_msgs["mdt"], 
                     "Please input an integer value for Number of Tessellations Cells"),
                   (self.text_id, 0): 
                    (t_msgs["mdt"], 
                     "Please input an integer value for Tessellation Identifier"),
                   (domain_container, 0): 
                    (t_msgs["mdt"], 
                     "Please input Domain"),
                   (self.text_fmax, range(0, 181, 180)): 
                    (t_msgs["incrt_val"], 
                     "Face Flatness should be less than 180"),
                   (len(filename4), 0): 
                    (t_msgs["mdt"], 
                     "Please input Output File Name")}

    for field in checkFields:
        if not field[1]:
            tkMessageBox.showinfo(checkFields[field][0], checkFields[field][1])
        else:
            if not (field[1][0] < field[0].get() < field[1][1]):
                tkMessageBox.showinfo(checkFields[field[0]], checkFields[field][1])