[Python] Condensing a Python method that does a different comparison depending on the operator...

Discussão em 'Python' iniciado por Stack, Janeiro 23, 2025.

  1. Stack

    Stack Membro Participativo

    I am trying to write a method that evaluates a statement, but the operator (>, <, =) is sent by the user. I am wondering if there is a easy way to write a more concise method.

    The simplified version of the code is:

    def comparsion(val1: int, val2: int, operator: str):

    if operator == ">":
    if val1 > val2:
    return True

    elif operator == "=":
    if val1 == val2:
    return True

    elif operator == "<":
    if val1 < val2:
    return True


    The only thing that changes in each case is the operator, I am wondering if there is a more efficient way to write this.

    Continue reading...

Compartilhe esta Página