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

时间:2013-10-24 05:51:27

标签: python

为什么这不起作用?

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

不打印

1 个答案:

答案 0 :(得分:1)

将字符串的名称从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的范围内。这不是真的。这就是为什么没有任何东西被打印出来。