My code isn’t working and I can’t see the reason. Here’s my code.
def sumHealth(enemies):
enemyHealth = 0
index = 0
while index < len(enemies):
enemyHealth = enemyHealth + enemies[index].health
index += 1
def sumHealth(friends):
friendHealth = 0
index = 0
while index < len(friends):
friendHealth = friendHealth + friends[index].health
index += 1
if friendHealth > enemyhealth:
hero.say("Attack")
def sumHealth(enemies):
enemyHealth = 0
index = 0
while index < len(enemies):
enemyTotalHealth = enemyHealth + enemies[index].health
index += 1
def sumHealth(friends):
friendHealth = 0
index = 0
while index < len(friends):
friendTotalHealth = friendHealth + friends[index].health
index += 1
if friendTotalHealth > enemyTotalHealth:
hero.say("Attack")
Is this easier to find the problem. Still doesn’t work.
I’m not sure what you are trying to do with this code. Let me explain your code:
In the function sumHealth you iterate enemies and summarise their health in enemyTotalHealth, then for each of the enemies you define again and again the function sumHealth (why???). That function checks if accumulating healths of friends are greater than enemyTotalHealth. But I don’t see where you call any of that functions.
I recommend you return to the Forest and revise levels about functions. Also is it the whole code?