AspiringSoftwareDeveloper - 2 years ago 55
Python Question

# why does my count value stay 0?

I have struggled for hte past hour trying to figure out why my "count" variable stays 0 when I run this code.

``````def main():

print "There are %s Instagram Routes in the DB" % db.routes.count()

results = defaultdict(dict)
d= 1.5 #distance
t= 4*3600 #in seconds
# For each route a_route in db.routes
print "Start of for each route for loop"
all_routes = db.routes.find().limit(5)

for a_route in all_routes:
print "restart"

# for a set of size 1 to N:
for i in range(1,a_route['n_points']+1):

# Create set of size n
set_of_size_i_pts = select_points(a_route,i)

# Calcalute the number of possible routes for the set
x_route = find_routes(set_of_size_i_pts,t,d)
x_length = len(set_of_size_i_pts)
results[a_route['_id']].update({x_length:x_route})

print results

def select_points(a_route,i):
pts = a_route['points']
return random.sample(pts,i)

def find_routes(set_of_size_i_pts,t,d):
all_routes = db.routes.find().limit(5)

count = 0
for a_route in all_routes:
if is_legitimate_route(set_of_size_i_pts,a_route,t,d):
print "hel"
count+=1
print "count: %s" % count
b = 6
print b
return count
``````

Output:

``````r

estart
hel
count: 1
6
hel
count: 1
6
hel
count: 1
6
hel
count: 1
6
hel
....
``````

Any advice would be really appreciated!

Thank you
The goal is to be able to count the number of true statements. This is why I increment by 1 each time.

I think this for loop 'for a_route in all_routes:' is executing only once whenever it is called from here 'x_route = find_routes(set_of_size_i_pts,t,d)'.

Check this in your code. And run it again. Also try the below code, See if that prints incremented count values or not. I have added a static number in for loop just to confirm that if your for loop executes more than one time then it will must print incremented count.

``````def main():

print "There are %s Instagram Routes in the DB" % db.routes.count()

results = defaultdict(dict)
d= 1.5 #distance
t= 4*3600 #in seconds
# For each route a_route in db.routes
print "Start of for each route for loop"
all_routes = db.routes.find().limit(5)

for a_route in all_routes:
print "restart"

# for a set of size 1 to N:
for i in range(1,a_route['n_points']+1):

# Create set of size n
set_of_size_i_pts = select_points(a_route,i)

# Calcalute the number of possible routes for the set
x_route = find_routes(set_of_size_i_pts,t,d)
x_length = len(set_of_size_i_pts)
results[a_route['_id']].update({x_length:x_route})

print results

def select_points(a_route,i):
pts = a_route['points']
return random.sample(pts,i)

def find_routes(set_of_size_i_pts,t,d):
all_routes = db.routes.find().limit(5)

count = 0
for a_route in range(1, 10):

print "hel"
count+=1
print "count: %s" % count
b = 6
print b
return count
``````
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download