[Python] How to limit a number to be within a specified range? (Python) [duplicate]

Discussão em 'Python' iniciado por Stack, Novembro 28, 2024.

  1. Stack

    Stack Membro Participativo

    I want to limit a number to be within a certain range. Currently, I am doing the following:

    minN = 1
    maxN = 10
    n = something() #some return value from a function
    n = max(minN, n)
    n = min(maxN, n)


    This keeps it within minN and maxN, but it doesn't look very nice. How could I do it better?

    PS: FYI, I am using Python 2.6.

    Continue reading...

Compartilhe esta Página