Fair Battle/ Python/ Help [Solved]

Yup. That should do it

My code still isn’t working the character doesn’t say anything ever

def healthSumEnemy(enemies):
    enemiesIndex = 0
    health = 0
    while enemiesIndex < enemies.len:
        enemy = enemies[enemiesIndex]
        enemiesIndex +=1
        health += enemy.health
    return health
def healthSumFriend(friends):
    friendsIndex = 0
    health = 0
    while friendsIndex < friends.len:
        friend = friends[friendsIndex]
        friendsIndex +=1
        health += friend.health
    return health 
while True:
    if healthSumFriend(friends) > healthSumEnemy(enemies):
        hero.say("Attack")
# Say "Attack" when you are ready.

hey there i’ve found the issue!

1 Like

actually compress the enemy function and friends function into one, like this

if it works, ill have to delete this
@Some_Guy

edit: it worked so i deleted it sorry to all others!

1 Like

Like this?

def sumHealth(enemies):
    totalHealth = 0
    unitsIndex = 0
    
    while unitsIndex < len(units):
        unit = units[unitsIndex]
        totalHealth += enemies.health
        
        unitsIndex += 1
        
    return totalHealth
def sumHealth(friends):
    friendsIndex = 0
    health = 0
    while friendsIndex < friends.len:
        friend = friends[friendsIndex]
        friendsIndex +=1
        health += friend.health
    return health 

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```
@Ashmit_Singh

no, just merge my code like this

# 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]
        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
1 Like

Oh i see thank you so much @Ashmit_Singh
instead of deleting everything can you leave the phrase “compress the definations” please just incase someone has the same problem

1 Like

ok then, save this code if it was helpful, also mark a solution

sorry to rebump it to the top but i had to say it:

me - comes and tries to help
(not working)
3 regulars - try to help
(not working)
me - writes some random last minute code
(working)

XD

2 Likes

yeah LoL im new to coding and this is the sum of my experience XD

1 Like

its ok you will get better

4 Likes

YoU RUINED THE CLOSING TOPIC XD JK

3 Likes

This topic was automatically closed 12 hours after the last reply. New replies are no longer allowed.