[Python] Python - If statements in optional parameters

Discussão em 'Python' iniciado por Stack, Maio 27, 2025.

  1. Stack

    Stack Membro Participativo

    I have a prewritten piece of code that says:

    setprecision = 0
    printit(100.123, precision=6 if setprecision is None else setprecision)


    My goal is to write a function that can handle all of these given parameters:

    def printit(*text, precision=2):
    printme = ''
    for arg in text:
    if (type(arg) is float) or (type(arg) is int):
    printme += str(f"{arg:.{precision}f}")
    else:
    printme += arg
    print(printme)


    This all works fine, except my function doesn't recognize or use the if statement at all, and just prints with a precision of 6. I have never seen a parameter like this before. How can I change my function to accept it, without changing the piece of code itself?

    Continue reading...

Compartilhe esta Página