2

我在一个似乎不起作用的函数中有一个嵌套循环。

“广播”字典的键和值都存在(非空)并打印到标准输出。

当我尝试在一个循环(第二个循环)中打印出我认为应该是广播的值时,它是空的 - 这对我来说没有意义,因为我清楚地看到它们在我使用时从第一个循环中打印出来

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 上提问,所以请放轻松:)

4

1 回答 1

0

我刚刚尝试了一个示例代码,我能够到达内部循环。这里:

di = {'0': ['a1', 'a2', 'a3'], '2': ['b1', 'b2', 'b3'], '3': 'c'}
for k,v in di.items():
    print k
    for item in v:
        print "\t" + item

输出:

$ python t.py
0
    a1
    a2
    a3
3
    c
2
    b1
    b2
    b3
$

您指定的值似乎没有正确形成的循环。你能检查一下吗。根据您提供的键/值,您的字典应该是这样的吗?您能否正确指定。

0: [[], 
2: [[(u'LosAngeles', kurt.Script([
    kurt.Block('whenClicked'), 
3: ... looping through broadcasts values []  
于 2013-09-10T05:59:26.150 回答