+ 2

I got a bug. Can anyone help me?

I was working with nested lists. I made these functions: def table(length, height): list = [] for i in range(height): list.append([]) for _ in range(length): list[i].append(0) return list def possible(length, height): table = table(length, height) list = [] for i in range(length): if table[0][i] == 0: list.append(i) return list This function should return a list of indexes, such that for every index in this list there should be a 0 in the first list of the table. But i get a bug: list index out of range Can anyone help me?

8th Jul 2021, 5:43 PM
Gašper Koželj
Gašper Koželj - avatar
7 Respostas
+ 5
Gašper Koželj There were a few things wrong with your original code which may not have been in the code you're working on. I modified the original code to resolve the name collisions. However, there are other overall improvements I would have considered. But it's not fully clear what you're attempting to do. https://code.sololearn.com/cxyrOybzzdIX/?ref=app Since you seem to have it working in your version of the code, I'll leave it alone.
8th Jul 2021, 7:16 PM
David Carroll
David Carroll - avatar
+ 1
What is length and height?
8th Jul 2021, 5:55 PM
minirkk
minirkk - avatar
0
Length is 7, height is 6 in example, but this is a part of a bigger project with random possibilities.
8th Jul 2021, 5:58 PM
Gašper Koželj
Gašper Koželj - avatar
0
The list returned by table is [[0], [0],...,[0]]
8th Jul 2021, 6:04 PM
Angelo
Angelo - avatar
0
Yeah i wrote the table wrong in the question. But it still doesnt work.
8th Jul 2021, 6:07 PM
Gašper Koželj
Gašper Koželj - avatar
0
What output does it give you? (and what output are you expecting?)
8th Jul 2021, 6:13 PM
Angelo
Angelo - avatar
0
I figured it out. I did it like this: row = table[0] for i in range(length): if row[i] ==0: List.append(i) return list
8th Jul 2021, 7:05 PM
Gašper Koželj
Gašper Koželj - avatar