Ich verwende eine for-Schleife, um eine mehrzeilige Zeichenfolge zu erstellen. Nachdem die Schleife fertig ist, möchte ich ein Popup-Fenster aufrufen und die Zeichenfolge anzeigen. Aus irgendeinem Grund ruft es das Popup-Fenster bei jeder Iteration auf, obwohl der Aufruf nach der for-Schleife erfolgt und korrekt eingerückt ist. Ich kann so etwas nicht online finden. Irgendwelche Ideen?Python: for-Schleife einschließlich Zeilen außerhalb der Schleife
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)
Der Ausgang ich erwartet habe, ist so etwas wie:
"Blue 1,4-2,5
Yellow 2,6-3,1"
Aber was ich bekomme ist:
"Blau 1,4-2,5
Blau 1,4-2,5
Yellow 2,6-3,1"
Falls erforderlich, hier ist die gesamte Funktion:
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)
Dies ist äußerst ungewöhnlich. Versuchen Sie, eine neue Zeile zwischen der letzten Zeile der for-Schleife und '' print string '' einzufügen. Überprüfen Sie auch alle Einrückungen. Der Dolmetscher ist manchmal langweilig. – manglano
Haben Sie versucht, dies ohne 'popup (string)' auszuführen? – Lafexlos
Können Sie Ihre Frage bearbeiten, um die Einrückungen anzuzeigen? Sie könnten es innerhalb einer for-Schleife oder etwas aufrufen. – jape