0

I'm trying to make a game using pygame but I'm having problems with lists inside other lists.

inside the class of Enemy/Enemy 2 i have the following code:

ei = [[Enemy(), Enemy()][Enemy2()]]

for wave in ei:
    if self in wave:
        print(ei.index(self))

The object is inside the list, I have checked by printing out ei in bulk, however this code just returns 'None'.

I tried to print('True') instead of the index, however it still prints None

numbermaniac
  • 788
  • 1
  • 13
  • 28
  • 2
    Please ensure the code you provided is properly formatted using the syntax highlighting tools, and ensure the indentation here is exactly as the code you have in your own editor. – idjaw Sep 05 '16 at 20:26
  • 2
    You may [edit] your question to show a [mcve]. We don't know what `self` is in the context you've given – OneCricketeer Sep 05 '16 at 20:28
  • Is Enemy and Enemy2 the same class or different classes? Where in the class is this code? In `\_\_init\_\_()`? Is it even in a method? Provide the necessary information and your question will be easier to answer. – Ted Klein Bergman Sep 06 '16 at 06:26
  • Possible duplicate of [Access item in a list of lists](http://stackoverflow.com/questions/18449360/access-item-in-a-list-of-lists) – numbermaniac Sep 06 '16 at 07:16
  • You missed a comma there? I assume that you want two `waves` (whatever those are) and the first one has two `Enemy` and the second one has a `Enemy2`. The biggest problem is, `self` won't be in `ei`, it would be in `wave`. If you want the index of the wave which contains `self` (not sure, just guessing) then... see my answer. But I am guessing. – MariusSiuram Sep 06 '16 at 08:26

1 Answers1

0

I am guessing, and there are several problems in your example. But maaaybe you want this:

for index, wave in enumerate(ei):
    if self in wave:
        print(index)

See my comments or elaborate your question --as already recommended by other users, provide a MCVE and fix/explain the variables and classes.

MariusSiuram
  • 3,380
  • 1
  • 21
  • 40