我在一个似乎不起作用的函数中有一个嵌套循环。
“广播”字典的键和值都存在(非空)并打印到标准输出。
当我尝试在一个循环(第二个循环)中打印出我认为应该是广播的值时,它是空的 - 这对我来说没有意义,因为我清楚地看到它们在我使用时从第一个循环中打印出来
print(broadcast.values())
下面的第一个块是我的代码,然后是打印到标准输出的内容:(...显示在输出继续的位置)
def broadcast_display(results):
broadcast = results['broadcast']
thumbnails = results['thumbnails']
html = []
html.append('\n<h2 style="text-align:center;">Broadcast / Receive</h2>')
message = ""
print('broadcasts keys')
print(broadcast.keys())
print('broadcasts values')
print(broadcast.values())
for type, lists in broadcast.items():
print('looping through broadcasts values')
print(lists)
for list in lists:
#Does not reach this loop
print('TEST 3: does not print')
#code goes on
html.append('\n<hr>')
html.append('\n<h2>{0}</h2>'.format(KelpPlugin.SCRIPT_TITLES[blocktype])) #heading
html.append('\n<table border = "1">')
for sprite, script in blocklist:
if KelpPlugin.script_start_type(script) == KelpPlugin.HAT_WHEN_I_RECEIVE:
# check if the message is the same as the last one
# if it is, print this script next to the last
# otherwise, print it below the last
if message != script[0].args[0].lower():
html.append('\n </tr>')
html.append('\n <tr>')
message = script[0].args[0].lower()
script_images = KelpPlugin.to_scratch_blocks(sprite, script)
html.append('\n<td>')
html.append('\n<p> {0}</p>'.format(sprite))
html.append('\n <p><img src="{0}" height="100" width="100"></p>'.format(thumbnails[sprite]))
html.append('\n<pre class="blocks">')
html.append('\n<p>{0}</p>'.format(script_images))
html.append('\n</pre>')
html.append('\n</td>')
elif KelpPlugin.script_start_type != KelpPlugin.NO_HAT:
if message == "":
html.append('\n </tr>')
html.append('\n <tr>')
script_images = KelpPlugin.to_scratch_blocks(sprite, script)
html.append('\n<p>{0}</p>'.format(sprite))
html.append('\n <p><img src="{0}" height="100" width="100></p>'.format(thumbnails[sprite]))
html.append('\n<pre class="blocks">')
html.append('\n<p>{0}</p>'.format(script_images))
html.append('\n</pre>')
html.append('\n </tr>')
html.append('\n</table>')
return ''.join(html)
broadcasts keys
[0, 2, 3]
broadcasts values
[[], [[(u'LosAngeles', kurt.Script([
kurt.Block('whenClicked'), ...
looping through broadcasts values
[]
结果字典如下所示:
{0: [], 2: [[(u'San Juan Batista', kurt.Script([
kurt.Block('whenClicked'),
kurt.Block('broadcast:', u'SJB')], pos=(69, 59.0))), (u'Missionary', kurt.Script([
kurt.Block('whenIReceive', u'SJB'), ... 3: []}
这也是我第一次在 StackOverflow 上提问,所以请放轻松:)