Python Help on "Fair Battle"

help please

# 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(units):
    totalHealth = 0
    # Complete this function:
    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.
    if sumHealth(friends) <= sumHealth(enemies):
        hero.say("Wait")
    # Say "Attack" when your side has more total health.
    else:
        hero.say("ATTACK!!!")

what am i doing wrong?

In your function, you are using comparative additionā€¦if test is true, then add to the counter. This means that after 1 or 2 units, the statement will always be false, so no further addition occurs.

Instead, do not use the comparative and just add the value to the counter.

Btw, not required but a handy tidbit. If you wanted to, you can simply the procedure by using:

        totalHealth += unit[unitsIndex].health

This is my code I am stuck.
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!!!ā€)

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

thanks I got it now.

This e-mail is for the sole use of the individual for whom it is intended. If you are neither the intended recipient, nor agent responsible for delivering this e-mail to the intended recipient, any disclosure, retransmission, copying, or taking action in reliance on this information is strictly prohibited. If you have received this e-mail in error, please notify the person transmitting the information immediately. All e-mail correspondence to and from this e-mail address may be subject to NC Public Records Law which result in monitoring and disclosure to third parties, including law enforcement. In compliance with federal laws, Guilford County Schools administers all educational programs, employment activities and admissions without discrimination because of race, religion, national or ethnic origin, color, age, military service, disability or gender, except where exemption is appropriate and allowed by law. Refer to the Board of Educationā€™s Discrimination Free Environment Policy AC for a complete statement. Inquiries or complaints should be directed to the Guilford County Schools Compliance Officer, 120 Franklin Boulevard, Greensboro, NC 27401; 336-370-2323

1 Like

Thereā€™s no need for targetIndex, youā€™ll only need to add and assign the hp. Otherwise you can do heeo.wait(12) then say attack if youā€™re lucky

1 Like