pythontkinterttk

How to simplify function with multiple if-statements and multiple conditions?


I have a function which should output which test scores are significantly higher than the other test scores. There are four tests (a, b, c and d). The user will input the test scores and check different checkbuttons if there are significant differences between any of the test scores. There are 6 checkbuttons in total: "a & b", "a & c", "a & d" and so on. The user will have access to all this info beforehand.

The Problem

My current function works, but in order to cover every eventuality I would have to write out many, many if-statements:

def my_function (A_test_score, B_test_score, C_test_score, D_test_score, A_B_state, A_C_state, A_D_state, B_C_state, B_D_state, C_D_state):
 
    A_test_score = int(a.get())
    B_test_score = int(b.get())
    C_test_score = int(c.get())
    D_test_score = int(d.get())
 
    # Retrieving state of the checkboxes (1 = checked)
    A_B_state = A_B_var.get()
    A_C_state = A_C_var.get()
    A_D_state = A_D_var.get()
    B_C_state = B_C_var.get()
    B_D_state = B_D_var.get()
    C_D_state = C_D_var.get()
 
    final_text = ""
 
    if A_B_state == 1:
        if (A_test_score - B_test_score) > 0:
            final_text += "A is significantly larger than B"
        elif (A_test_score - B_test_score) < 0:
            final_text += "B is significantly larger than A"
 
    if A_C_state == 1:
        if (A_test_score - C_test_score) > 0:
            final_text += "\nA is significantly larger than C"
        elif (A_test_score - C_test_score) < 0:
            final_text += "\nC is significantly larger than A"
 
… # and so on…
 
    if A_B_state == 1 and (A_test_score - B_test_score) > 0 and A_C_state == 1 and (A_test_score - C_test_score) > 0 and A_D_state == 1 and (A_test_score - D_test_score) > 0:
        return "A is significantly larger than B, C and D"
 
    if A_B_state == 1 and (A_test_score - B_test_score) > 0 and A_C_state == 1 and (A_test_score - C_test_score) > 0:
        return "A is significantly larger than B and C"
 
    return final_text

Disered Output

If for example the score on test a and b are significantly larger than c and d: "a and b are significantly larger than c and d."

And if, for example, a is larger than b, c and d, and b is larger than d: "a is significantly larger than b, c and d. b is larger than d."

Thus, the goal is to avoid redundant outputs like this: "a is significantly larger than b. a is significantly larger than c. a is significantly larger than d."

I'm guessing there is a better way to to this... Can't find anything in the previous suggested posts so far. Any thoughts?

Thanks!


Solution

  • You can use a list result_a to store items that is less than A. Same for B, C and D. Then you can use these lists to construct the final text:

    def my_function():
        A_test_score = int(a.get())
        B_test_score = int(b.get())
        C_test_score = int(c.get())
        D_test_score = int(d.get())
     
        # Retrieving state of the checkboxes (1 = checked)
        A_B_state = A_B_var.get()
        A_C_state = A_C_var.get()
        A_D_state = A_D_var.get()
        B_C_state = B_C_var.get()
        B_D_state = B_D_var.get()
        C_D_state = C_D_var.get()
    
        # lists to store checking result
        result_a = []
        result_b = []
        result_c = []
        result_d = []
    
        if A_B_state:
            if A_test_score > B_test_score:
                result_a.append("B")
            elif B_test_score > A_test_score:
                result_b.append("A")
    
        if A_C_state:
            if A_test_score > C_test_score:
                result_a.append("C")
            elif C_test_score > A_test_score:
                result_c.append("A")
    
        if A_D_state:
            if A_test_score > D_test_score:
                result_a.append("D")
            elif D_test_score > A_test_score:
                result_d.append("A")
    
        if B_C_state:
            if B_test_score > C_test_score:
                result_b.append("C")
            elif C_test_score > B_test_score:
                result_c.append("B")
    
        if B_D_state:
            if B_test_score > D_test_score:
                result_b.append("D")
            elif D_test_score > B_test_score:
                result_d.append("B")
    
        if C_D_state:
            if C_test_score > D_test_score:
                result_c.append("D")
            elif D_test_score > C_test_score:
                result_d.append("C")
    
        # list to construct the final text
        final_result = []
    
        if result_a:
            final_result.append(f"A is significantly larger than {' and '.join(result_a)}")
    
        if result_b:
            final_result.append(f"B is significantly larger than {' and '.join(result_b)}")
    
        if result_c:
            final_result.append(f"C is significantly larger than {' and '.join(result_c)}")
    
        if result_d:
            final_result.append(f"D is significantly larger than {' and '.join(result_d)}")
    
        # return the final text
        return "\n".join(final_result)