有没有一种方法可以使我更有效地编写此代码?

时间:2020-02-21 07:42:54

标签: python if-statement coding-efficiency

您知道如何编写此代码以使用更复杂和更少的行吗? 尤其是正在执行指令的正文部分似乎非常多余。

谢谢。

 z = """Wählen Sie eine der folgenden vorgegebenen Operationen: (add/subtract/multiply/divide/end/history)
    """
    a = "Erste Zahl: "
    b = "Zweite Zahl: "
    ops = ["add","subtract","multiply","divide"]
    list = []
    answer = input(z)

    def add(x,y):
        return x+y
    def sub(x,y):
        return x-y
    def mult(x,y):
        return x*y
    def div(x,y):
        return x/y

    while answer in ops:
        if answer == "add":
            print("Resultat: ",add(int(input(a)),int(input(b))))
            list.append(answer)
            answer = input(z)
        elif answer == "subtract":
            print("Resultat: ",sub(int(input(a)),int(input(b))))
            list.append(answer)
            answer = input(z)
        elif answer == "multiply":
            print("Resultat: ",mult(int(input(a)),int(input(b))))
            list.append(answer)
            answer = input(z)
        elif answer == "divide":
            print("Resultat: ",div(int(input(a)),int(input(b))))
            list.append(answer)
            answer = input(z)

    if answer == "history":
        print(list)
        answer = input(z)

    elif answer == "end":
        print("Das Programm wird beendet.")

    else:
        print("""Geben Sie bitte eine gültige Eingabe ein.""")
        answer = input(z)

最好, 詹卢卡

2 个答案:

答案 0 :(得分:2)

您可以定义操作数和函数之间的映射。在C或C ++中,我们将其称为函数指针,在C#中,我们将其称为Action或lambda。无论如何,看起来像这样,我为此重复使用了ops

ops = {"add":add, "subtract":sub, "multiply":mult, "divide":div}

接下来,您可以将add(...)之类的函数调用替换为ops[answer](...)

    if answer == "add":
        print("Resultat: ",ops[answer](int(input(a)),int(input(b))))
        list.append(answer)
        answer = input(z)

好东西:这适用于所有功能。因此,您可以摆脱所有ifelif语句:

z = """Wählen Sie eine der folgenden vorgegebenen Operationen: (add/subtract/multiply/divide/end/history)
"""
a = "Erste Zahl: "
b = "Zweite Zahl: "

list = []
answer = input(z)


def add(x, y):
    return x + y


def sub(x, y):
    return x - y


def mult(x, y):
    return x * y


def div(x, y):
    return x / y


ops = {"add": add, "subtract": sub, "multiply": mult, "divide": div}

while answer in ops:
    print("Resultat: ", ops[answer](int(input(a)), int(input(b))))
    list.append(answer)
    answer = input(z)

if answer == "history":
    print(list)
    answer = input(z)

elif answer == "end":
    print("Das Programm wird beendet.")

else:
    print("""Geben Sie bitte eine gültige Eingabe ein.""")
    answer = input(z)

还请注意,一旦您输入“历史记录”,您的程序就可能无法按预期运行,但是我将把该修复程序留给您,因为它与问题无关。

来自评论:

个数字输入尚未存储到我的列表中。我该如何做而不必重新调用中断程序的输入(a)/输入(b)?

您可以将数字存储在变量中,并在向历史记录中添加文本时使用它们:

while answer in ops:
    number1 = int(input(a))
    number2 = int(input(b))
    result = ops[answer](number1, number2)
    list.append(f"{number1} {answer} {number2} = {result}")
    print("Resultat: ", result)
    answer = input(z)

输出为:

history
['3 add 4 = 7', '8 subtract 2 = 6']

答案 1 :(得分:-2)

尝试一下:

while answer in ops:
    if answer == "add":
        print("Resultat: ",add(int(input(a)),int(input(b))))
    elif answer == "subtract":
        print("Resultat: ",sub(int(input(a)),int(input(b))))
    elif answer == "multiply":
        print("Resultat: ",mult(int(input(a)),int(input(b))))
    elif answer == "divide":
        print("Resultat: ",div(int(input(a)),int(input(b))))

   list.append(answer)
   answer = input(z)