2016-03-04 219 views
1

我想知道是否有更好的写作方法。下面陈述的当前代码有效。我只是想知道是否有更好的方法。嵌套的IF-ELIF语句

这是用于验证。因此,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() 
+0

如果这段代码正在工作,那么这个问题似乎更适合[Code Review StackExchange site](http://codereview.stackexchange.com)。 – ShadowRanger

+0

@ShadowRanger:谢谢你,我从来不知道这样的网站存在。我仍然是Pyton2.7的新手。我也会在那里发帖。谢谢:) –

回答

1

您通常不需要检查len == 0,只是测试的对象“感实性”。

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() 
+0

嗨,约翰,感谢您的代码...但是,我实际上寻找的是消除必须验证“文件名4”一次内部循环一次,一次外部循环。 –

+0

@AlyAbdelaziz,好吧看看现在 –

+0

注意:如果我不做“len”检查,程序不会忽略输入字段是否留空并给出错误。所以首先我检查是否空,如果不是,那么我检查整数值。 –

2

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

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])