繁体   English   中英

Python-如何检查值是否在给定的float / integer范围内?

[英]Python - how to check if the value is within given range of float/integer?

为什么这不起作用?

reflace=0
input = "55,1,1,1".split(",")

# Now find the first field from comma
input[reflace] = round( float( input[reflace] ) , 2) + 0.01
ranges = [(-1,1)]
if any(lower <= input[reflace] <= upper for (lower, upper) in ranges):
  print "+ " + input

不打印

将字符串的名称从input更改为其他名称。 我正在使用inputData

reflace=0
inputData = "55,1,1,1".split(",")
# Now find the first field from comma
inputData[reflace] = round( float( inputData[reflace] ) , 2) + 0.01

print inputData[reflace] ### Will print 55.01

ranges = [(-1,1)]
if any(lower <= inputData[reflace] <= upper for (lower, upper) in ranges):
  print "+ " + inputData

因此,您正在检查55.01是否在-1,1的范围内。这是不正确的。 这就是为什么什么都没打印出来的原因。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM