4

To aid in some mental rehabilitation after a nasty hip accident I decided to try and teach myself to program in Python. So I've just started getting my head around defining functions and classes.

I have a basic enemy class that allows me to create an object like this.

enemy01=Enemy("Goblin",10,100,2,5,1,2)

To get the enemy name I can use

foe=enemy01.get_enemyName()

My problem is I want to use a list of enemies which I append as they get killed off and the variable 'foe' to refer to whatever enemy is in play.

So I tried creating a list of the enemy objects, like

currentEnemy=[enemy01, enemy02, enemy03]

and do

foe=currentEnemy.....

But I cant work out how to attach the .get_enemyName()

I was trying things like this to concatenate it

foe=(currentEnemy, ".get_enemyName()")

But nothing I am trying is working when I type 'print(foe)' which is what would be in the main body of code.

I have tried searching online and here but it's really hard as a beginner to put it into words what I am trying to do. Maybe i'm just going about it the wrong way to start with.

I hope I'm making sense and thanks for reading :) Simon

jamylak
135k30 gold badges238 silver badges240 bronze badges
asked Feb 17, 2018 at 14:12
1
  • Welcome to StackOverflow! Happy to hear that programming can help you in your healing process! Could you show us the code you wrote by editing and adding it to your question? Also please take the time to read about minimal verifiable example stackoverflow.com/help/mcve Commented Feb 17, 2018 at 14:17

3 Answers 3

2

You seem to conflate the list of all enemies and a variable referring to a particular one.

Here's what you could do:

enemies = [enemy01, enemy02, enemy03]
for currentEnemy in enemies:
 eName = currentEnemy.get_enemyName()
 print('The current enemy is', eName)

Later on you would probably decide that creating individual enemies is too tedious and use another loop for that:

# create three identical goblins
enemies = [Enemy("Goblin",10,100,2,5,1,2) for _ in range(3)]
answered Feb 17, 2018 at 14:17
Sign up to request clarification or add additional context in comments.

2 Comments

Yes this!, but the loop doesn't seem right. Possibly random some values from different diffulties stored in some dictionary. But yeah the "approach" is correct.
Thanks - that worked in terms of pulling the enemy out the list and getting its name, but it did the entire list so maybe I never explained myself right. I only need the first entry on the list, which will be the enemy the player is fighting in an arena style combat - then after the enemy is dead I remove item 0 and enemy02 is next inline, and so the loop continues until the list is empty or the player dead.
2

If your class :

class Enemy:
 def __init__(self,name):
 self.name=name
 def get_enemyName(self,):
 return self.name

and you have initialised currentEnemy as :

enemy01=Enemy('Goblin')
enemy02=Enemy('Rhino')
enemy03=Enemy('DR.Octopus')
currentEnemy=[enemy01, enemy02, enemy03]

And you want to get list of all enemy names into a list foe. Then try :

foe=[x.get_enemyName() for x in currentEnemy]
print foe

i.e. list comprehensions

i= foe.index('Rhino') #find index of Rhino
del[currentEnemy[i]] #let's kill Rhino
print 'current enemies at play {}'.format([x.get_enemyName() for x in currentEnemy])
answered Feb 17, 2018 at 14:35

Comments

1

Sorry - real silly error on my part. I just missed the list index.

I sorted it

enemylist = [enemy01, enemy02, enemy03]

currentEnemy=enemylist[0]

foe=currentEnemy.get_enemyName()

print(foe)

answered Feb 17, 2018 at 15:20

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.