I believe that my code is written properly, however even when my team has more health than the enemies, they fight and still lose, please see my code below.
def healthSum(target):
targetIndex = 0
health = 0
while targetIndex < target.length:
target = target[targetIndex]
targetIndex +=1
health += target.health
return health
while True:
friends = hero.findFriends()
enemies = hero.findEnemies()
# Calculate and compare the total health of your soldiers and the ogres.
if healthSum(friends) > healthSum(enemies):
hero.say("Attack")
# Say "Attack" when you are ready.
I changed it as you suggested, and it is still not working for me.
def healthSum(target):
targetIndex = 0
health = 0
while targetIndex < len(target):
target = target[targetIndex]
targetIndex +=1
health += target.health
return health
while True:
friends = hero.findFriends()
enemies = hero.findEnemies()
friendHealth = healthSum(friends)
enemyHealth = healthSum(enemies)
hero.say(friendHealth)
hero.say(enemyHealth)
# Calculate and compare the total health of your soldiers and the ogres.
if friendHealth > enemyHealth:
# Say "Attack" when you are ready.
hero.say("Attack")
def healthSum(target):
targetIndex = 0
health = 0
while targetIndex < len(target):
target = target[targetIndex]
targetIndex +=1
health += target.health
return health
I see the problem.
The parameter name you’re passing in is: target, however you are overwriting it inside the loop, which will cause problems. Instead rename the parameter to targets since you’re passing in an array, and update the while loop accordingly.
Hi, thanks for making this post in the first place, I’ve encountered the same problem somehow…
Could you help me to check if there is anything wrong with my code? Many thanks
# Attack when your soldiers' total health is greater
# than the ogres' total health
# Write a function that takes an array of units as input
# And returns the sum of all the units' health
def sumHealth(units):
totalHealth = 0
unitsIndex = 0
while unitsIndex < len(units):
unit = units[unitsIndex]
if unit.health > totalHealth:
totalHealth += unit.health
unitsIndex += 1
return totalHealth
while True:
friends = hero.findFriends()
enemies = hero.findEnemies()
# Get the total health of your soldiers and the ogres.
soldierHealth = sumHealth(friends)
ogreHealth = sumHealth(enemies)
# Say "Attack" when your side has more total health.
if soldierHealth > ogreHealth:
hero.say("Attack")
pass
Do you know what that means? It means if the unit’s health is less than or equal to the totalHealth already added up, it won’t get added to the total. (just remove the if loop while keeping the totalHealth += unit.health part
I’m not sure what I’m doing wrong but my code isn’t working. My hero keeps saying attack right after an ogre keeps showing up.
# Attack when your soldiers' total health is greater
# than the ogres' total health
# This function return the sum of all the units' health.
def sumHealth(target):
totalHealth = 0
targetIndex = 0
# Complete this function:
while targetIndex < len(target):
target = target[targetIndex]
totalHealth += target.health
targetIndex += 1
return totalHealth
while True:
friends = hero.findFriends()
enemies = hero.findEnemies()
# Get the total health of your soldiers and the ogres.
if sumHealth(friends) <= sumHealth(enemies):
hero.say("Wait")
# Say "Attack" when your side has more total health.
else:
hero.say("ATTACK!!!")
Can someone please help. It also doesn’t help that I don’t completely understand arrays.
Look at your sumHealth function. The input for the function is supposed to be an array (so targets, not target). You got confused within the while True loop and defined target = target[targetIndex]. Switch the input of sumHealth function to targets (with an s, so you don’t confuse yourself), switch the target in len(target), and switch the target in front of [targetIndex] to targets too. Then run the code again. It should work out.
Thanks @SuperSmacker. That helped. I’ve really got to better at arrays. That has always been where I struggle in programming even when I was in college courses. I get how they work but when going through them that’s where I get lost especially when trying to understand how to set priority and stuff.