kit kit - 4 months ago 14
Python Question

How to find number is between some specified range in python?

I want to find the number which is input for the Python function. The input value is not fixed; it may be in thousands, lacks or cores.

I'm using Python 2.7.

Example:

def check(number):

if(10000 <= number <= 50000):
print "Number is between 10000 & 50000"

if(50001 <= number <= 100000):
print "Number is between 50001 & 100000"

if(100000 <= number <= 500000):
print "Number is between 100000 & 500000"

if __name__ == "__main__":

with open("input.csv", "r+b") as f:
m = mmap.mmap(f.fileno(), 0)
reader = csv.DictReader(iter(m.readline, ""))
for read in reader:
num = read['time']
check(num)#calling check function


input.csv -

time
404907
404863
404345
403630
403562
120279146
120279128
216870
58460


I'm reading input.csv file. and I want to check if the
time
column is in a specific range, but it is not working properly.

What am I missing?

Answer

You have to check int(num) instead of just num, because you need to compare integer vs integer
Also seems there is a typo error in your third condition

   if(100000 <= number <= 500000):
      print "Number is between 100000 & 500000

I supposed you should have the following line:

   if(100001 <= number <= 500000):
      print "Number is between 100001 & 500000"`  
Comments