# Your goal is to protect Reynaldo
# Find the paladin with the lowest health.
def lowestHealthPaladin():
lowestHealth = 99999
lowestFriend = None
friends = hero.findFriends()
for friend in friends:
if friend.type != "paladin":
continue
if friend.health < lowestHealth and friend.health < friend.maxHealth:
lowestHealth = friend.health
lowestFriend = friend
return lowestFriend
def commandPaladin(paladin):
# Heal the paladin with the lowest health using lowestHealthPaladin()
# You can use paladin.canCast("heal") and command(paladin, "cast", "heal", target)
# Paladins can also shield: command(paladin, "shield")
# And don't forget, they can attack, too!
while True:
enemy = hero.findNearestEnemy()
lowest = lowestHealthPaladin()
if paladin.canCast("heal"):
hero.command(paladin, "cast", "heal", lowest)
if enemy:
hero.attack(enemy)
pass
def commandGriffin(griffin):
while True:
enemy = hero.findNearestEnemy()
if enemy:
hero.command(griffin, "attack", enemy)
def commandPeasant(peasant):
while True:
item = hero.findNearestItem()
if item and item.type == "coin":
hero.command(peasant, "move", item.pos)
def commandFriends():
# Command your friends.
friends = hero.findFriends()
for friend in friends:
if friend.type == "peasant":
#commandPeasant(friend)
pass
elif friend.type == "griffin-rider":
commandGriffin(friend)
pass
elif friend.type == "paladin":
commandPaladin(friend)
while True:
commandFriends()
# Summon griffin riders
if hero.gold > hero.costOf("griffin-rider"):
hero.summon("griffin-rider")
At a quick glance, I notice that you have a While True: loop in the Command functions. Once you get to that, you don’t have a way to get out and will keep trying to command the Paladin to heal or the griffin to attack and nothing else. You either want to remove the While True loop in the functions (best method), or create a way to break out of it.
Like, would I have to make while loops that aren’t while true loops, because I did fix it, but now it says my code never finished. Here it is
# Your goal is to protect Reynaldo
# Find the paladin with the lowest health.
def lowestHealthPaladin():
lowestHealth = 99999
lowestFriend = None
friends = hero.findFriends()
for friend in friends:
if friend.type != "paladin":
continue
elif friend.health < lowestHealth and friend.health < friend.maxHealth:
lowestHealth = friend.health
lowestFriend = friend
return lowestFriend
def commandPaladin(paladin):
# Heal the paladin with the lowest health using lowestHealthPaladin()
# You can use paladin.canCast("heal") and command(paladin, "cast", "heal", target)
# Paladins can also shield: command(paladin, "shield")
# And don't forget, they can attack, too!
while paladin.health < paladin.maxhealth:
enemy = hero.findNearestEnemy()
lowest = lowestHealthPaladin()
if paladin.canCast("heal") and lowest:
hero.command(paladin, "cast", "heal", lowest)
elif enemy:
hero.attack(enemy)
pass
def commandGriffin(griffin):
while griffin.health > 0:
enemy = hero.findNearestEnemy()
if enemy:
hero.command(griffin, "attack", enemy)
def commandPeasant(peasant):
while peasant.health > 0:
item = peasant.findNearestItem()
if item and item.type == "coin":
hero.command(peasant, "move", item.pos)
def commandFriends():
# Command your friends.
friends = hero.findFriends()
for friend in friends:
if friend.type == "peasant":
commandPeasant(friend)
pass
elif friend.type == "griffin-rider":
commandGriffin(friend)
pass
elif friend.type == "paladin":
commandPaladin(friend)
while True:
commandFriends()
# Summon griffin riders
if hero.gold > hero.costOf("griffin-rider"):
hero.summon("griffin-rider")
You don’t want to include while loops at all in the functions. The functions are designed to provide a command to the friend then go back to the main loop, until they are given a different command. You don’t need to continually command them within the function. The While True loop in the main line of code will ensure you keep looping through all your friends and command them as directed. So remove all the While loops in the functions, shift your indentation and see where your code gets you then. It should at least start commanding them, but you may need to tweak the commands to complete the level.
Correct, the functions with the commands for each group don’t need a loop.
The commandFriends() function isolates one friend at a time, then grabs the command for that friend from the specific function for the friend type. Once that friend knows what to do, it will continue to do that command until it is commanded differently when you go through the commandFriends() for loop again. A friend can only follow one command at at time and will do that until the command is changed.