I have tried so many times and even reproduced some of the code found on here. I’m almost at the point of giving up can someone please help me with this code! Any help is appreciated…
#Commented out to stop infinite loop.
# You can't reach your friends to defend them!
# Tell them to go home where the archer can help.
while True:
weakestFriend = None
leastHealth = 9999
friendIndex = 0
# Find which friend has the lowest health.
friends = hero.findFriends()
while friendIndex < len(friends):
friend = friends[friendIndex]
if friend:
if friend.health < leastHealth and friend.type != "archer":
weakestFriend = friend
leastHealth = friend.health
friendIndex += 1
pass
# Tell the weakest friends to go home first.
if weakestFriend:
hero.say('Hey ' + weakestFriend.id + ', go home!')
pass
Actually, the friend.type is required, otherwise you’ll target the archers too. The indentation for friendIndex does need to be corrected tho. Also, here:
# Tell the weakest friends to go home first.
if weakestFriend:
hero.say('Hey ' + weakestFriend.id + ', go home!')
pass
Typically, the preexisting comments are placed (indented) where you need to have the code. Those three lines need to line up with the comment. (You can just delete the ‘pass’ line if you want…it’s just a place holder, so it not required.)
while True:
weakestFriend = None
leastHealth = 9999
friendIndex = 0
# Find which friend has the lowest health.
friends = hero.findFriends()
while friendIndex < len(friends):
friend = friends[friendIndex]
if friend:
if friend.health < leastHealth and friend.type != "archer":
weakestFriend = friend
leastHealth = friend.health
friendIndex += 1
pass
# Tell the weakest friends to go home first.
if weakestFriend:
hero.say('Hey ' + weakestFriend.id + ', go home!')
pass