I have a nested loop inside a function that does not seem to be working.
Both the keys and values of the 'broadcast' dictionary exist (are non-empty) and print to stdout.
When I try to print out in a loop (the second loop) what I think should be broadcast's values, it is empty - this does not make sense to me because I clearly see them printed out from the first loop when I use
print(broadcast.values())
The first chunk below is my code and following it is what is printed out to stdout: ( ... displayed where output continues)
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
[]
The results dictionary looks like this:
{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: []}
Also this is my first time asking a question on StackOverflow so go easy on me please :)
I just tried with a sample code and I am able to reach the inner loop. Here:
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
Output:
$ python t.py
0
a1
a2
a3
3
c
2
b1
b2
b3
$
The values that you specify seem to not have a properly formed loop. Can you check that. As per the key/values that you have provided, here is how your dictionary should look like? Can you please specify that properly.
0: [[],
2: [[(u'LosAngeles', kurt.Script([
kurt.Block('whenClicked'),
3: ... looping through broadcasts values []
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.