Python Help on "Fair Battle"

Howdy and welcome to the forum! When posting code, please be sure to format it properly. You can learn how to do so here: [Essentials] How To Post/Format Your Code Correctly. For this post, I’ve formatted it for you.

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)
    # Get the total health of your soldiers and the ogres.
    if friendHealth <= enemyHealth:
        hero.say("wait")
    # Say “Attack” when your side has more total health.
    elif friendHealth > enemyHealth:
        hero.say("ATTACK!!!")

The problem is with your naming scheme in the function. You start with def healthSum(target). You then define ‘target’ again in the code…you are redefining an object as a part of itself. Instead, make your array name plural, so it is distinctive…this also helps to indicate it contains multiple objects. Then, make the same change to the 2 other instances where you are referencing that array.

1 Like