2016-03-04 185 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])