Problem with indexing?

    def getZomName(self, i):
        x = None
        x = i.getIntoNode().getName()
        lengthEntry = len((str(x)))-1
        zomNum = str(x)[1:lengthEntry]
        print(x)
        print(zomNum)
        zomName = self.zombie[(int(zomNum))]
        return ZomName

This gets a zombies name from a collision entry. So the zombies name is “z0” I want to get 0 as ZomNum and run it through self.zombie list to get the name of the zombie.

It gives me:

Known pipe types:
  wglGraphicsPipe
(all display modules loaded.)
:display(warning): FrameBufferProperties available less than requested.
Traceback (most recent call last):
  File "C:\Users\Lisa\Documents\NetBeansProjects\Zombie_Arena\src\module.py", line 206, in detectShots
    zombieName = self.getZomName(x)
  File "C:\Users\Lisa\Documents\NetBeansProjects\Zombie_Arena\src\module.py", line 238, in getZomName
    zomName = self.zombie[(int(zomNum))]
ValueError: invalid literal for int() with base 10: ''
:task(error): Exception occurred in PythonTask detect shots
Traceback (most recent call last):
  File "C:\Users\Lisa\Documents\NetBeansProjects\Zombie_Arena\src\zombie_arena.py", line 11, in <module>
    gameApp.run()
  File "C:\Panda3D-1.7.2\direct\showbase\ShowBase.py", line 2630, in run
    self.taskMgr.run()
  File "C:\Panda3D-1.7.2\direct\task\Task.py", line 502, in run
    self.step()
  File "C:\Panda3D-1.7.2\direct\task\Task.py", line 460, in step
    self.mgr.poll()
  File "C:\Users\Lisa\Documents\NetBeansProjects\Zombie_Arena\src\module.py", line 206, in detectShots
    zombieName = self.getZomName(x)
  File "C:\Users\Lisa\Documents\NetBeansProjects\Zombie_Arena\src\module.py", line 238, in getZomName
    zomName = self.zombie[(int(zomNum))]
ValueError: invalid literal for int() with base 10: ''
z0

It is supposed to print x and zomNum, but it only prints one thing, not sure which one it is though.

Thanks.

It looks as though you likely want “lengthEntry = len(x)”, not “len(x)-1”.

From the Python reference:

By the way, if I may ask, why do you cast to str? The result of getName() should already be a string, I would imagine. (The brackets in the print statements are also superfluous, by the way, although I could see your including those as a matter of style preference.)

You can simply omit the second slice index on a string and it will go to the end of the string.

mystring = 'a987'
num = mystring[1:]
# value of num is '987'
   return self.ZomName
AttributeError: Application instance has no attribute 'ZomName'

What’s going on? I tried pre defining it.

Case is important. In the code you posted you defined a local identifier zomName:

zomName = self.zombie[(int(zomNum))]

That’s zomName. Not ZomName. Not self.ZomName.

And if you are simply going to return it on the next line, you can skip the assignment altogether and just put:

return self.zombie[(int(zomNum))]