如何在 Python 中查找数字是否属于特定范围?
问题描述
假设我想检查 x 是否属于 0 到 0.5 的范围.我该怎么做?
Suppose I want to check if x belongs to range 0 to 0.5. How can I do it?
解决方案
不,你不能那样做.range() 需要整数参数.如果您想知道 x 是否在此范围内,请尝试以下某种形式:
No, you can't do that. range() expects integer arguments. If you want to know if x is inside this range try some form of this:
print 0.0 <= x <= 0.5
小心你的上限.如果您使用 range() 它被排除在外(range(0, 5) 不包括 5!)
Be careful with your upper limit. If you use range() it is excluded (range(0, 5) does not include 5!)
相关文章