How should I handle inclusive ranges in Python?

后端 未结 12 2227
天涯浪人
天涯浪人 2020-12-14 05:56

I am working in a domain in which ranges are conventionally described inclusively. I have human-readable descriptions such as from A to B , which represent rang

12条回答
  •  眼角桃花
    2020-12-14 06:15

    I am not sure if this is already covered, this is how I handled it to check if my variable is within a defined range:

    my var=10 # want to check if it is in range(0,10) as inclusive
    limits = range(0,10)
    limits.append(limits[-1]+1)
    if(my_var in limits):
        print("In Limit")
    else:
        print("Out of Limit")
    

    This code will return "In Limit" since I have expanded my range by 1 hence making it inclusive

提交回复
热议问题