once the if is satisfied, the remaining chain of elif's will not get executed. That's the way that it is intended.
If you want each statement to be executes, use a list of if's
the else is not necessary as you initialize sum to 0 before starting
Now, since sum is replaced each time, sum will be overwritten by the last successful if statement.
example:
If you want each statement to be executes, use a list of if's
the else is not necessary as you initialize sum to 0 before starting
Now, since sum is replaced each time, sum will be overwritten by the last successful if statement.
example:
def method1(e8, e13, e21, e55): sum = 0 print(f"\nmethod1") if (e8 > e13): print(f"e8 > e13, so I'm done") sum = 1 elif (e8 < e13): print(f"e8 < e13, so I'm done") sum = 0 elif (e13 > e21): print(f"e813 > e21, so I'm done") sum = 1 elif (e13 < e21): print(f"e13 < e21, so I'm done") sum = 0 elif (e21 > e55): print(f"e21 > e55, so I'm done") sum = 1 print(f"sum: {sum}") def method2(e8, e13, e21, e55): sum = 0 print(f"\nmethod2") if (e8, e13, e21, e55): print(f"e8 > e13") sum = 1 if (e8 < e13): print(f"e8 < e13") sum = 0 if (e13 > e21): print(f"e813 > e21") sum = 1 if (e13 < e21): print(f"e13 < e21") sum = 0 if (e21 > e55): print(f"e21 > e55") sum = 1 print(f"sum: {sum}") def main(): method1(e8 = 1, e13 = 10, e21=6, e55 = 4) method2(e8 = 1, e13 = 10, e21=6, e55 = 4) if __name__ == '__main__': main()
Output:method1
e8 < e13, so I'm done
sum: 0
method2
e8 > e13
e8 < e13
e813 > e21
e21 > e55
sum: 1
which is what should be expected.