2016-04-05 14 views
-2

私はforループを使用して複数行の文字列を作成しています。ループが終了したら、ポップアップウィンドウを呼び出して文字列を表示したいと思います。何らかの理由で、呼び出しがforループの後で正しくインデントされていても、各繰り返しでポップアップウィンドウを呼び出します。私はこのようなものをオンラインで見つけることはできません。何か案は?Python:forループ(ループ外の行を含む)

string = "" 
for color in highlight_report_dict: 
    if highlight_report_dict[color] == []: 
     continue 
    else: 
     string += '{}\n'.format(color) 
     for item in highlight_report_dict[color]: 
      start = str(int(float(item[0])))+'.0' #changes '3.34' to '3.0', etc for any index 
      end = str(int(float(item[1])))+'.0' 
      start = index_key[start] 
      end = index_key[end] 
      string += '{} - {}\n'.format(start, end) 
print string #Why does it call popup() during for loop??? 
popup(string) 

私は期待していた出力のようなものです:

"ブルー 1.4から2.5

イエロー 2.6から3.1"

しかし、私は取得していますが次のとおりです。

"ブルー 1.4-2.5

ブルー 1.4から2.5

イエロー 2.6から3.1"

必要な場合は、ここで全体の機能は次のとおりです。

def highlight_report(): 
    tag_list = t.tag_names()[1:] #creates list of all highlight tag colors, removing 'ins' tag 
    start = '1.0' 
    end = t.index(END) 

    highlight_report_dict = {} 

    for color in tag_list: 
     highlight_report_dict[color] = [] 
     cycle = True 
     while cycle: 
      highlight_report_dict[color].append(t.tag_nextrange(color, start, end)) 

      try: 
       start = highlight_report_dict[color][-1][1] #uses previous tag end as new start 
      except IndexError: 
       start = '1.0' 
       cycle = False 
       if highlight_report_dict[color][-1] ==(): #removes empty tuple created at end of cycle 
        del highlight_report_dict[color][-1] 


    string = "" 
    for color in highlight_report_dict: 
     if highlight_report_dict[color] == []: 
      continue 
     else: 
      string += '{}\n'.format(color) 
      for item in highlight_report_dict[color]: 
       start = str(int(float(item[0])))+'.0' #changes '3.34' to '3.0', etc for any index 
       end = str(int(float(item[1])))+'.0' 
       start = index_key[start] 
       end = index_key[end] 
       string += '{} - {}\n'.format(start, end) 
    print string #Why does it call popup() during for loop??? 
    popup(string) 
+0

これは非常に珍しいことです。 forループの最後の行と ''印刷文字列 ''の間に改行を挿入してみてください。また、すべてのインデントを確認してください。通訳は時には正気ではない。 – manglano

+0

'popup(string)'なしでこれを実行しようとしましたか? – Lafexlos

+0

インデントを表示するために質問を編集できますか? forループや何かの中で呼び出すかもしれません。 – jape

答えて

0

はそれを考え出しました。これはタブと4つのスペースの問題でした。あるエディタからSublime Textにテキストをコピーしました。いくつかのテキストはタブ付きで、一部は4つのスペースでした。私はSublimeで4つのスペースにすべてのタブを変換して、今私のforループは期待どおりに動作します。

関連する問題