Marvin Pfaffenhöfer Marvin Pfaffenhöfer - 24 days ago 8
Python Question

Trouble with a for-loop

Sorry my english isnt really good so i try to explain it short,
this is my code:

self.boni_new = {}

for asdf in range(0,5):
for i in xrange(0,5):
vas = 5-i
self.boni_new[asdf] = {}
self.boni_new[asdf][5-i] = {}
self.boni_new[asdf][5-i][0] = ui.TextLine()
self.boni_new[asdf][5-i][0].SetParent(self)
self.boni_new[asdf][5-i][0].SetText("Bonus "+str(5-asdf))
self.boni_new[asdf][5-i][0].SetPosition(15,10+25*(vas-1))
self.boni_new[asdf][5-i][0].Hide()
self.boni_new[asdf][5-i][1] = DropDown(self,"- Keiner -")
self.boni_new[asdf][5-i][1].SetPosition(70,10+25*(vas-1))
for x in AFFECT_DICT:
if x in BONI_AVAIL:
self.boni_new[asdf][5-i][1].AppendItem(str(AFFECT_DICT[x](0)),x)
self.boni_new[asdf][5-i][1].SetSize(330,20)
self.boni_new[asdf][5-i][1].Hide()
self.boni_new[asdf][5-i][2] = ui.Bar("UI")
self.boni_new[asdf][5-i][2].SetParent(self)
self.boni_new[asdf][5-i][2].SetPosition(410,10+25*(vas-1))
self.boni_new[asdf][5-i][2].SetColor(0xC0000000)
self.boni_new[asdf][5-i][2].SetSize(80,20)
self.boni_new[asdf][5-i][2].Hide()
self.boni_new[asdf][5-i][3] = Edit2("0",14)
self.boni_new[asdf][5-i][3].SetParent(self.boni_new[asdf][5-i][2])
self.boni_new[asdf][5-i][3].SetNumberMode()
self.boni_new[asdf][5-i][3].SetSize(80,20)
self.boni_new[asdf][5-i][3].SetPosition(4,3)
self.boni_new[asdf][5-i][3].Hide()
dbg.TraceError(str(self.boni_new))


i want to get this:

{0: {**1**: {0: <ui.TextLine object at 0x08EEC930>, 1: <switchbot.DropDown object at 0x08F42B70>, 2: <ui.Bar object at 0x08F467D0>, 3: <switchbot.Edit2 object at 0x08F467F0>}}, 1: {**1**: {0: <ui.TextLine object at 0x08F46870>, 1: <switchbot.DropDown object at 0x08F62A90>, 2: <ui.Bar object at 0x08F686F0>, 3: <switchbot.Edit2 object at 0x08F68710>}}, 2: {**1**: {0: <ui.TextLine object at 0x08F68790>, 1: <switchbot.DropDown object at 0x052DD9B0>, 2: <ui.Bar object at 0x052E4610>, 3: <switchbot.Edit2 object at 0x052E4630>}}, 3: {**1**: {0: <ui.TextLine object at 0x052EFFD0>, 1: <switchbot.DropDown object at 0x052FF8D0>, 2: <ui.Bar object at 0x052FC530>, 3: <switchbot.Edit2 object at 0x052FC550>}}, 4: {**5**: {0: <ui.TextLine object at 0x052FC5D0>, 1: <switchbot.DropDown object at 0x052FC5F0>, 2: <ui.Bar object at 0x05301270>, 3: <switchbot.Edit2 object at 0x05301290>}}}
{0: {**2**: {0: <ui.TextLine object at 0x08EEC930>, 1: <switchbot.DropDown object at 0x08F42B70>, 2: <ui.Bar object at 0x08F467D0>, 3: <switchbot.Edit2 object at 0x08F467F0>}}, 1: {**2**: {0: <ui.TextLine object at 0x08F46870>, 1: <switchbot.DropDown object at 0x08F62A90>, 2: <ui.Bar object at 0x08F686F0>, 3: <switchbot.Edit2 object at 0x08F68710>}}, 2: {**2**: {0: <ui.TextLine object at 0x08F68790>, 1: <switchbot.DropDown object at 0x052DD9B0>, 2: <ui.Bar object at 0x052E4610>, 3: <switchbot.Edit2 object at 0x052E4630>}}, 3: {**2**: {0: <ui.TextLine object at 0x052EFFD0>, 1: <switchbot.DropDown object at 0x052FF8D0>, 2: <ui.Bar object at 0x052FC530>, 3: <switchbot.Edit2 object at 0x052FC550>}}, 4: {**4**: {0: <ui.TextLine object at 0x052FC5D0>, 1: <switchbot.DropDown object at 0x05301290>, 2: <ui.Bar object at 0x05301ED0>, 3: <switchbot.Edit2 object at 0x05301EF0>}}}
{0: {**3**: {0: <ui.TextLine object at 0x08EEC930>, 1: <switchbot.DropDown object at 0x08F42B70>, 2: <ui.Bar object at 0x08F467D0>, 3: <switchbot.Edit2 object at 0x08F467F0>}}, 1: {**3**: {0: <ui.TextLine object at 0x08F46870>, 1: <switchbot.DropDown object at 0x08F62A90>, 2: <ui.Bar object at 0x08F686F0>, 3: <switchbot.Edit2 object at 0x08F68710>}}, 2: {**3**: {0: <ui.TextLine object at 0x08F68790>, 1: <switchbot.DropDown object at 0x052DD9B0>, 2: <ui.Bar object at 0x052E4610>, 3: <switchbot.Edit2 object at 0x052E4630>}}, 3: {**3**: {0: <ui.TextLine object at 0x052EFFD0>, 1: <switchbot.DropDown object at 0x052FF8D0>, 2: <ui.Bar object at 0x052FC530>, 3: <switchbot.Edit2 object at 0x052FC550>}}, 4: {**3**: {0: <ui.TextLine object at 0x052FC5D0>, 1: <switchbot.DropDown object at 0x05301EF0>, 2: <ui.Bar object at 0x05309B50>, 3: <switchbot.Edit2 object at 0x05309B70>}}}
{0: {**4**: {0: <ui.TextLine object at 0x08EEC930>, 1: <switchbot.DropDown object at 0x08F42B70>, 2: <ui.Bar object at 0x08F467D0>, 3: <switchbot.Edit2 object at 0x08F467F0>}}, 1: {**4**: {0: <ui.TextLine object at 0x08F46870>, 1: <switchbot.DropDown object at 0x08F62A90>, 2: <ui.Bar object at 0x08F686F0>, 3: <switchbot.Edit2 object at 0x08F68710>}}, 2: {**4**: {0: <ui.TextLine object at 0x08F68790>, 1: <switchbot.DropDown object at 0x052DD9B0>, 2: <ui.Bar object at 0x052E4610>, 3: <switchbot.Edit2 object at 0x052E4630>}}, 3: {**4**: {0: <ui.TextLine object at 0x052EFFD0>, 1: <switchbot.DropDown object at 0x052FF8D0>, 2: <ui.Bar object at 0x052FC530>, 3: <switchbot.Edit2 object at 0x052FC550>}}, 4: {**2**: {0: <ui.TextLine object at 0x052FC5D0>, 1: <switchbot.DropDown object at 0x05309B70>, 2: <ui.Bar object at 0x0530F7D0>, 3: <switchbot.Edit2 object at 0x0530F7F0>}}}
{0: {**5**: {0: <ui.TextLine object at 0x08EEC930>, 1: <switchbot.DropDown object at 0x08F42B70>, 2: <ui.Bar object at 0x08F467D0>, 3: <switchbot.Edit2 object at 0x08F467F0>}}, 1: {**5**: {0: <ui.TextLine object at 0x08F46870>, 1: <switchbot.DropDown object at 0x08F62A90>, 2: <ui.Bar object at 0x08F686F0>, 3: <switchbot.Edit2 object at 0x08F68710>}}, 2: {**5**: {0: <ui.TextLine object at 0x08F68790>, 1: <switchbot.DropDown object at 0x052DD9B0>, 2: <ui.Bar object at 0x052E4610>, 3: <switchbot.Edit2 object at 0x052E4630>}}, 3: {**5**: {0: <ui.TextLine object at 0x052EFFD0>, 1: <switchbot.DropDown object at 0x052FF8D0>, 2: <ui.Bar object at 0x052FC530>, 3: <switchbot.Edit2 object at 0x052FC550>}}, 4: {**1**: {0: <ui.TextLine object at 0x052FC5D0>, 1: <switchbot.DropDown object at 0x0530F7F0>, 2: <ui.Bar object at 0x0547D450>, 3: <switchbot.Edit2 object at 0x0547D470>}}}


but i get this:

{0: {**1**: {0: <ui.TextLine object at 0x08EEC930>, 1: <switchbot.DropDown object at 0x08F42B70>, 2: <ui.Bar object at 0x08F467D0>, 3: <switchbot.Edit2 object at 0x08F467F0>}}, 1: {**1**: {0: <ui.TextLine object at 0x08F46870>, 1: <switchbot.DropDown object at 0x08F62A90>, 2: <ui.Bar object at 0x08F686F0>, 3: <switchbot.Edit2 object at 0x08F68710>}}, 2: {**1**: {0: <ui.TextLine object at 0x08F68790>, 1: <switchbot.DropDown object at 0x052DD9B0>, 2: <ui.Bar object at 0x052E4610>, 3: <switchbot.Edit2 object at 0x052E4630>}}, 3: {**1**: {0: <ui.TextLine object at 0x052EFFD0>, 1: <switchbot.DropDown object at 0x052FF8D0>, 2: <ui.Bar object at 0x052FC530>, 3: <switchbot.Edit2 object at 0x052FC550>}}, 4: {**5**: {0: <ui.TextLine object at 0x052FC5D0>, 1: <switchbot.DropDown object at 0x052FC5F0>, 2: <ui.Bar object at 0x05301270>, 3: <switchbot.Edit2 object at 0x05301290>}}}
{0: {**1**: {0: <ui.TextLine object at 0x08EEC930>, 1: <switchbot.DropDown object at 0x08F42B70>, 2: <ui.Bar object at 0x08F467D0>, 3: <switchbot.Edit2 object at 0x08F467F0>}}, 1: {**1**: {0: <ui.TextLine object at 0x08F46870>, 1: <switchbot.DropDown object at 0x08F62A90>, 2: <ui.Bar object at 0x08F686F0>, 3: <switchbot.Edit2 object at 0x08F68710>}}, 2: {**1**: {0: <ui.TextLine object at 0x08F68790>, 1: <switchbot.DropDown object at 0x052DD9B0>, 2: <ui.Bar object at 0x052E4610>, 3: <switchbot.Edit2 object at 0x052E4630>}}, 3: {**1**: {0: <ui.TextLine object at 0x052EFFD0>, 1: <switchbot.DropDown object at 0x052FF8D0>, 2: <ui.Bar object at 0x052FC530>, 3: <switchbot.Edit2 object at 0x052FC550>}}, 4: {**4**: {0: <ui.TextLine object at 0x052FC5D0>, 1: <switchbot.DropDown object at 0x05301290>, 2: <ui.Bar object at 0x05301ED0>, 3: <switchbot.Edit2 object at 0x05301EF0>}}}
{0: {**1**: {0: <ui.TextLine object at 0x08EEC930>, 1: <switchbot.DropDown object at 0x08F42B70>, 2: <ui.Bar object at 0x08F467D0>, 3: <switchbot.Edit2 object at 0x08F467F0>}}, 1: {**1**: {0: <ui.TextLine object at 0x08F46870>, 1: <switchbot.DropDown object at 0x08F62A90>, 2: <ui.Bar object at 0x08F686F0>, 3: <switchbot.Edit2 object at 0x08F68710>}}, 2: {**1**: {0: <ui.TextLine object at 0x08F68790>, 1: <switchbot.DropDown object at 0x052DD9B0>, 2: <ui.Bar object at 0x052E4610>, 3: <switchbot.Edit2 object at 0x052E4630>}}, 3: {**1**: {0: <ui.TextLine object at 0x052EFFD0>, 1: <switchbot.DropDown object at 0x052FF8D0>, 2: <ui.Bar object at 0x052FC530>, 3: <switchbot.Edit2 object at 0x052FC550>}}, 4: {**3**: {0: <ui.TextLine object at 0x052FC5D0>, 1: <switchbot.DropDown object at 0x05301EF0>, 2: <ui.Bar object at 0x05309B50>, 3: <switchbot.Edit2 object at 0x05309B70>}}}
{0: {**1**: {0: <ui.TextLine object at 0x08EEC930>, 1: <switchbot.DropDown object at 0x08F42B70>, 2: <ui.Bar object at 0x08F467D0>, 3: <switchbot.Edit2 object at 0x08F467F0>}}, 1: {**1**: {0: <ui.TextLine object at 0x08F46870>, 1: <switchbot.DropDown object at 0x08F62A90>, 2: <ui.Bar object at 0x08F686F0>, 3: <switchbot.Edit2 object at 0x08F68710>}}, 2: {**1**: {0: <ui.TextLine object at 0x08F68790>, 1: <switchbot.DropDown object at 0x052DD9B0>, 2: <ui.Bar object at 0x052E4610>, 3: <switchbot.Edit2 object at 0x052E4630>}}, 3: {**1**: {0: <ui.TextLine object at 0x052EFFD0>, 1: <switchbot.DropDown object at 0x052FF8D0>, 2: <ui.Bar object at 0x052FC530>, 3: <switchbot.Edit2 object at 0x052FC550>}}, 4: {**2**: {0: <ui.TextLine object at 0x052FC5D0>, 1: <switchbot.DropDown object at 0x05309B70>, 2: <ui.Bar object at 0x0530F7D0>, 3: <switchbot.Edit2 object at 0x0530F7F0>}}}
{0: {**1**: {0: <ui.TextLine object at 0x08EEC930>, 1: <switchbot.DropDown object at 0x08F42B70>, 2: <ui.Bar object at 0x08F467D0>, 3: <switchbot.Edit2 object at 0x08F467F0>}}, 1: {**1**: {0: <ui.TextLine object at 0x08F46870>, 1: <switchbot.DropDown object at 0x08F62A90>, 2: <ui.Bar object at 0x08F686F0>, 3: <switchbot.Edit2 object at 0x08F68710>}}, 2: {**1**: {0: <ui.TextLine object at 0x08F68790>, 1: <switchbot.DropDown object at 0x052DD9B0>, 2: <ui.Bar object at 0x052E4610>, 3: <switchbot.Edit2 object at 0x052E4630>}}, 3: {**1**: {0: <ui.TextLine object at 0x052EFFD0>, 1: <switchbot.DropDown object at 0x052FF8D0>, 2: <ui.Bar object at 0x052FC530>, 3: <switchbot.Edit2 object at 0x052FC550>}}, 4: {**1**: {0: <ui.TextLine object at 0x052FC5D0>, 1: <switchbot.DropDown object at 0x0530F7F0>, 2: <ui.Bar object at 0x0547D450>, 3: <switchbot.Edit2 object at 0x0547D470>}}}


I really dont know where is my issue and i hope someone can tell me how to solve my problem...

Answer Source

The main problem is that you are overwriting self.boni_new[asdf] with {} each time through the inner loop. Move that line before the inner loop.

You can iterate directly over the "reverse" sequence instead of defining vas = 5 - i, since you never use i otherwise.

You can also simplify your code with better use of temporary variables.

self.boni_new = {}

for asdf in range(0,5):
    self.boni_new[asdf] = {}
    for vas in xrange(5,0,-1):
        d = self.boni_new[asdf][vas] = {}
        d[0] = ui.TextLine()
        d[0].SetParent(self)
        d[0].SetText("Bonus "+str(5-asdf))
        d[0].SetPosition(15,10+25*(vas-1))
        d[0].Hide()

        d[1] = DropDown(self,"- Keiner -")
        d[1].SetPosition(70,10+25*(vas-1))
        for x, f in AFFECT_DICT.iteritems():
            if x in BONI_AVAIL:
                d[1].AppendItem(str(f(0)), x)
        d[1].SetSize(330,20)
        d[1].Hide()

        d[2] = ui.Bar("UI")
        d[2].SetParent(self)
        d[2].SetPosition(410,10+25*(vas-1))
        d[2].SetColor(0xC0000000)
        d[2].SetSize(80,20)
        d[2].Hide()

        d[3] = Edit2("0",14)
        d[3].SetParent(self.boni_new[asdf][vas][2])
        d[3].SetNumberMode()
        d[3].SetSize(80,20)
        d[3].SetPosition(4,3)
        d[3].Hide()
        dbg.TraceError(str(self.boni_new))