2016-05-16 10 views
0

私はこのコードを実行すると、なぜ私の "count"変数が0のままであるのか理解しようと、過去1時間に苦労しました。なぜ私のカウント値は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

「is_legitimate_route」は何をしていますか? [mcve]を入力してください。 –

+2

これは実際のコードですか?なぜなら、この '1'はそれぞれの' hel'の後にどこに印刷されているのかわからないからです。 – AKS

+0

前の点に加えて、なぜ7が印刷されていないのですか? –

答えて

1

これは、 'for_route in all_routes:'のループで '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 
+1

ありがとう!基本的には:どういたしまして – AspiringSoftwareDeveloper

+0

それが唯一の真の1xに評価しました。 –

関連する問題