如何将浮点数与Python中的其他两个浮点数进行比较?
当我进行以下操作以比较小数值是否在两个值之间时,我会得到不一致的结果。
x = 1.87
if 1.0 >= x < 2.0:
print("x: ", x, "; cat: high")
return 'high'
elif 2.0 >= x < 3.0:
print("x: ", x, "; cat: medium")
return 'medium'
elif 3.0 >= x <= 5.0:
print("x: ", x, "; cat: low")
return 'low'
else:
return None
结果,我获得了中
结果。我检查了其他值,而它们都不匹配。我在Math.isclose()
上找到了文章,但这对我没有帮助。我该如何解决?
When I do the following to compare if the decimal value is between the two values, I get inconsistent results.
x = 1.87
if 1.0 >= x < 2.0:
print("x: ", x, "; cat: high")
return 'high'
elif 2.0 >= x < 3.0:
print("x: ", x, "; cat: medium")
return 'medium'
elif 3.0 >= x <= 5.0:
print("x: ", x, "; cat: low")
return 'low'
else:
return None
I get the medium
returned as a result. I checked with other values, and none of them matched. I found articles on math.isclose()
, but it doesn't help me. How do I solve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论