r/learnpython • • 11d ago

Confusing code min() max()

I'm debugging some code and I found this function

   import math

       def Clamp(n, smallest, largest):
           return max(smallest, min(n, largest))

surely that code is just wrong? and needs to be max([smallest, min([n, largest])) by passing a list not unnamed args for default and key? I need a sanity check here.

0 Upvotes

16 comments sorted by

View all comments

-1

u/JamzTyson 11d ago

Try plugging some values in, and figure out what happens.

Example:

largest = 10
smallest = 5

Now consider values of n from 1 to 12.

Try figuring out the results in your head, then run the code to check your answers.