Home > Back-end >  Simplifying if statements in errors output
Simplifying if statements in errors output

Time:12-22

Have multiply if statements for my error output in project. Can it be simplified?

if self.Lot_Mean_r<=0 or self.Lot_SD_r<=0 or self.SSB_DF_r<=0 or self.SSW_DF_r<=0 or self.SST_DF_r<=0:
            if self.Lot_Mean_r<=0: 
                self.ui.textEdit_status_N.append("Error: Lot Mean negative or less than 0")
            if self.Lot_SD_r<=0:         
                self.ui.textEdit_status_N.append("Error: Lot SD negative or less than 0")
            if self.SSB_DF_r<=0:         
                self.ui.textEdit_status_N.append("Error: SSB DF negative or less than 0")   
            if self.SSW_DF_r<=0:         
                self.ui.textEdit_status_N.append("Error: SSW DF negative or less than 0")  
            if self.SST_DF_r<=0:         
                self.ui.textEdit_status_N.append("Error: SST DF negative or less than 0") 
else:  [main code]

CodePudding user response:

One possible way would be to go with a dict :

attr_dict = {"Lot Mean": self.Lot_Mean_r,
            "Lot SD": self.Lot_SD_r,
            "SSB DF": self.SSB_DF_r,
            "SSW DF": self.SSW_DF_r,
            "SST DF": self.SST_DF_r}

for key, value in attr_dict.items() :
    if value <= 0 :
        self.ui.textEdit_status_N.append(f"Error: {key} negative or less than 0")
  • Related