如何用多个if语句和多个条件来简化函数?

问题描述 投票:0回答:1

我有一个函数应该输出哪些测试分数明显高于其他测试分数。有四个测试(a、b、c 和 d)。用户将输入测试分数并检查不同的复选按钮,如果任何测试分数之间存在显着差异。总共有 6 个检查按钮:“a & b”、“a & c”、“a & d”等。用户将可以事先访问所有这些信息。

问题

我当前的函数可以工作,但为了涵盖所有可能发生的情况,我必须写出很多很多 if 语句:

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

离散输出

例如,如果测试 a 和 b 的分数明显大于 c 和 d: “a 和 b 明显大于 c 和 d。”

例如,如果 a 大于 b、c 和 d,并且 b 大于 d: “a 明显大于 b、c 和 d。 b 比 d 大。”

因此,目标是避免像这样的冗余输出: “a 明显大于 b。 a 明显大于 c。 a 明显大于 d。”

我猜有一个更好的方法来做到这一点...到目前为止,在之前的建议帖子中找不到任何内容。有什么想法吗?

谢谢!

python tkinter ttk
1个回答
0
投票

首先,您应该始终将“数据验证条件”放在方法的开头。我所说的“数据验证条件”是指您检查的条件,以便能够正确运行该方法需要运行的任何内容。

例如:

# in an imaginary case where 9 is not a number you want to compute
def add_but_not_nine(a, b):
    result = a + b
    if a == 9 or b == 9:
        return "can't compute nines"
    return result

可读性不太好,然而:

# in an imaginary case where 9 is not a number you want to compute
def add_but_not_nine(a, b):
    if a == 9 or b == 9:
        return "can't compute nines"
    return a + b

这样更好。这不是一个很好的例子,但我希望它能传达信息。

因此,在您的情况下,您应该将代码更改为:

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()
    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"
    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…
 
    
 
    return final_text

我认为你可以做出其他改进,但这才是对我来说真正突出的地方。

© www.soinside.com 2019 - 2024. All rights reserved.