def findStrongestEnemy(enemies):
strongest = None
strongestHealth = 0
enemyIndex = 0
while enemyIndex < len(enemies):
enemy = enemies[enemyIndex]
if enemy.health > strongestHealth:
strongestHealth = enemy.Health
enemyIndex += 1
return strongest
enemies = hero.findEnemies()
leader = findStrongestEnemy(enemies)
if leader:
hero.say(leader)
it says strongest is not defined
AnSeDra
September 11, 2020, 1:21pm
2
Why do you call a function in that function?
Andrei
it was at the beginning with strongest = none
So there is no strongest enemy?
def findStrongestEnemy(enemies):
strongest = enemy
strongestHealth = 0
enemyIndex = 0
# While enemyIndex is less than the length of enemies:
while enemyIndex < len(enemies):
# Set an enemy variable to enemies[enemyIndex]
enemy = enemies[enemyIndex]
# If enemy.health is greater than strongestHealth
if enemy.health > strongestHealth:
# Set `strongest` to enemy
# Set strongestHealth to enemy.health
strongestHealth = enemy.Health
# Increment enemyIndex
enemyIndex += 1
return strongest
enemies = hero.findEnemies()
leader = findStrongestEnemy(enemies)
if leader:
hero.say(leader)
even when I put enemy it said strongest was not defined
AnSeDra
September 11, 2020, 1:26pm
7
It is because your function not return anything.
Andrei
so what should I put for the strongest = ?
AnSeDra
September 11, 2020, 1:30pm
9
avery_witte:
strongest = enemy
And where do you say who the enemy is here?
Andrei
1 Like
ok I am confused about what your trying to say here
1 Like
AnSeDra
September 11, 2020, 1:39pm
11
There the enemy is None, so strongest is None too.
Andrei
1 Like
so I should put the strongest but how do I put the strongest if I don’t know who is the strongest
1 Like
AnSeDra
September 11, 2020, 1:41pm
13
This is what this loop should find out.
Andrei
but how do I get the strongest enemy from that loop
AnSeDra
September 11, 2020, 1:44pm
15
Look closer at this comment:
avery_witte:
# Set
strongest to enemy
Andrei
so you mean the strongest = enemy
sets the strongest to equal as if it were a regular enemy but the strongest enemy
AnSeDra
September 11, 2020, 2:08pm
17
So do you understand now how to solve the level?
Andrei
AnSeDra
September 11, 2020, 2:28pm
19
Again, why do you not return anything from the function? And you did not correct this:
Look closer at this comment:
Andrei
Andrei
so I figured out three options to put enemy leader and strongest but none of them are working so what do I do about?