为什么我的计数值保持为0?

时间:2016-05-16 05:14:01

标签: python

我一直在努力争取过去一小时试图找出为什么我的“count”变量在运行此代码时保持为0。

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

输出:

r

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

任何建议都会非常感激!

谢谢 目标是能够计算真实陈述的数量。这就是我每次增加1的原因。

1 个答案:

答案 0 :(得分:1)

我认为这是针对all_routes中的a_route循环播放的:'从这里调用时只执行一次' x_route = find_routes(set_of_size_i_pts,t,d)'。

在您的代码中查看此内容。并再次运行它。同时尝试下面的代码,看看是否打印增加的计数值。我在for循环中添加了一个静态数字,以确认如果你的for循环执行多次,那么它必须打印递增计数。

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
相关问题