5

I have a sample piece of code below that I need some help on. The code sets the 'outcome' variable to False in the beginning and only should become True if all the 'if' conditions are met. Is there a more efficient way of doing this? I am trying to avoid nested 'if' statements.

Thanks!

 outcome = False
 while True:
 if a != b:
 print("Error - 01")
 break
 if a["test_1"] != "test_value":
 print("Error - 02")
 break
 if "test_2" not in a:
 print("Error - 03")
 break
 if a["test_3"] == "is long data string":
 print("Error - 04")
 break
 outcome = True
 break
asked Oct 30, 2019 at 6:05
11
  • 1
    I think readability counts here more than performance, but I could be wrong. performance Commented Oct 30, 2019 at 6:08
  • 1
    if statement and statement and statement : did you try this? Commented Oct 30, 2019 at 6:09
  • I could use 'and' but would need specific error codes for each condition. :( Commented Oct 30, 2019 at 6:17
  • @RaymondC. This is probably related Commented Oct 30, 2019 at 6:22
  • 1
    why is all of that in a while loop? neither a nor b change... Commented Oct 30, 2019 at 6:22

3 Answers 3

4

I would write it like this, so the function ends once it encounters an error, and the most likely error should be on top, and if it encounters that error, it will return False and end the function. Else, it will check for all the other errors and then eventually conclude that the outcome is indeed True.

# testOutcome returns a boolean
outcome = testOutcome(a,b)
# Expects a and b
# Breaks out of the function call once error happens
def testOutcome(a,b):
 # Most likely error goes here
 if a != b:
 print("Error - 01")
 return False
 # Followed by second most likely error
 elif a["test_1"] != "test_value":
 print("Error - 02")
 return False
 # Followed by third most likely error
 elif "test_2" not in a:
 print("Error - 03")
 return False
 # Least likely Error
 elif a["test_3"] == "is long data string":
 print("Error - 04")
 return False
 else:
 return True
answered Oct 30, 2019 at 6:31
Sign up to request clarification or add additional context in comments.

1 Comment

Interesting, I did not think of it this way. Thanks :)
1

Or another way :

 outcome = True
 if a != b:
 print("Error - 01")
 outcome &= False
 if a["test_1"] != "test_value":
 print("Error - 02")
 outcome &= False
 if "test_2" not in a:
 print("Error - 03")
 outcome &= False
 if a["test_3"] == "is long data string":
 print("Error - 04")
 outcome &= False
answered Oct 30, 2019 at 13:44

2 Comments

Hi, sorry for the late reply. Just wanted to say thanks :) Question: Why do you use &= instead of = ?
Oh, I made it complicated, you can use = instead. Thanks
1

Hackiest way of doing this as per user request, not recommended tho, There are many ways to use structures if you are only looking to understand them. Functions like zip or list comprehension can be used once you convert to list Used list to keep positions intact.

a = {'test_1' : "test_value", 'test_4' : "None", 'test_3' : "long data string"}
b = {'test_1' : "test_value", 'test_4' : "None", 'test_3' : "long data string"}
# storing bool values, expecting True
test1 = [a==b,"test_2" in a,a["test_1"] == "test_value", a["test_3"] != "is long data string" ]
# storing error codes for False conditions 
result = ["Error - 01", "Error - 02", "Error - 03", "Error - 04"]
if False in test1:
 print(result[int(test1.index(False))])
else:
 print(True)
Error - 02
[Program finished]
answered Mar 11, 2021 at 11:29

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.