I have numerous comparisons to make. I have used multiple if statements for such, but there are too many and I'm not sure if it's the best coding practice. What can I use to replace them?
For example, I have this:
if ((ANum==2) and (Action==1)):
print ("*some text*")
if ((ANum==2) and (Action==1) and (2.5<=Freq<=4)):
print("*some text*")
if ((ANum==2) and (1<=FreqMagnitude<=6.5)):
print("*some text*")
if ((ANum==1) and (Action==0) and (4.5>Freq)):
print("*some text*")
I have like 20 of these statements with different single, double, or triple conditionals. Is there a better coding practice?
A good practice, without removing the if's, its to organice a little:
From this:
if ((ANum==2) and (Action==1) and (2.5<=Freq<=4)):
print("*some text*")
if ((ANum==2) and (1<=FreqMagnitude<=6.5)):
print("*some text*")
if ((ANum==1) and (Action==0) and (4.5>Freq)):
print("*some text*")
To this:
if(Action==1):
if(ANum==2):
if(1<=FreqMagnitude<=6.5):
print("*some text*")
if(2.5<=Freq<=4):
print("*some text*")
if(Action==0):
if(ANum==1):
if(4.5>Freq):
print("*some text*")
So if you have another criteria for action ==1 and ANum == 2 you have to only add a new if after the "ANum==2" validation.
The tip here is: Identify the "common" criteria and place them at the top, like "coming from general to specific criteria".
If you don't like this, you can try "switch case", but I don't know if the switch supports multiple criteria.