Raymond C.
Raymond C.

Reputation: 598

Multiple if conditions, without nesting

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

Upvotes: 5

Views: 3225

Answers (3)

Subham
Subham

Reputation: 411

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]

Upvotes: 1

Hank
Hank

Reputation: 21

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

Upvotes: 1

Dumb chimp
Dumb chimp

Reputation: 454

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

Upvotes: 4

Related Questions