In the code below, 'else' is same for all 'if' statements? Is there a way to have just one 'else' statement, rather than repeating the same code in all 'if' statement blocks for 'else' statement?
Here's example:
if condition A == "Yes":
print("A")
if condition B == "Yes":
print("B")
else:
print("D")
if condition C =="Yes":
print("C")
else:
print("D")
else:
print("D")
CodePudding user response:
You look like in a nested testing sequence
You test A if A ok, you test B if B ok you test C
if any test fail you print D
so ... you eitheir leave the testing sequence test everything print all Yes result or print D if any test fail (wich look like a big ERROR message)
or you keep the ugly if else sequence
CodePudding user response:
if you go for testing evything you can creat a function
def TestThis(Value, id )
if value == "Yes":
print(id)
return 1
else:
return 0
and call it like that
if TestThis(A,"A") == 0:
integrity = 1
Elif TestThis(B,"B") == 0:
integrity = 1
Elif TestThis(C,"C") == 0:
integrity = 1
if integrity == 1 :
print("D")
And I'm pretty sure you can find a way to finally get your sequence testing.
Edited to create that sequencial testing that was the aim from the start