在python中检查函数参数的范围

mar*_*rou 1 python arguments exception function

如何处理数字上有效但物理上超出范围的函数?

原因是,如果物理上正确的范围还剩,我希望我的程序告诉我并停止。

我考虑过将ValueError异常用于此错误处理。

例:

def return_approximation(T):
  #return T only if it is inbetween 0 < T < 100
  return T
Run Code Online (Sandbox Code Playgroud)

mki*_*ver 5

Python assert对于此类参数限制具有-statement。

def return_approximation(T):
    assert 0 < T < 100, "Argument 'T' out of range"
    return T
Run Code Online (Sandbox Code Playgroud)