def summonSoldier():
# Fill in code here to summon a soldier if you have enough gold
if self.gold >= self.costOf("soldier"):
self.summon("soldier")
else:
break
# This function commands your soldiers to attack their nearest enemy.
def commandSoldiers():
soldiers = self.findFriends()
if soldiers:
for soldier in self.findFriends():
target = soldier.findNearest(soldier.findEnemies())
if target:
self.command(soldier, "attack", target)
def pickUpNearestCoin():
while self.gold < self.costOf("soldier"):
coin = self.findNearest(self.findItems())
self.moveXY(coin.pos.x, coin.pos.y)
# In your loop, you can "call" the functions defined above.
# The following line causes the code inside the "pickUpNearestCoin" function to be executed.
pickUpNearestCoin()
# Call summonSoldier here
summonSoldier()
#Call commandSoldiers here
commandSoldiers()
while True:
pickUpNearestCoin()
summonSoldiers()
commandSoldiers()
def pickUpNearestCoin():
items = hero.findItems()
nearestCoin = hero.findNearest(items)
if nearestCoin:
hero.move(nearestCoin.pos)
# This function has your hero summon a soldier.
def summonSoldier():
# If hero.gold is greater than the cost of the "soldier":
if hero.gold>hero.costOf("soldier"):
# Then summon a "soldier":
hero.summon("soldier")
pass
# This function commands your soldiers to attack their nearest enemy.
def commandSoldiers():
for soldier in hero.findFriends():
enemy = soldier.findNearestEnemy()
if enemy:
hero.command(soldier, "attack", enemy)
while True:
# In your loop, you can "call" the functions defined above.
# The following line causes the code inside the "pickUpNearestCoin" function to be executed.
pickUpNearestCoin()
# Call summonSoldier here
summonSoldier()
# Call commandSoldiers here
commandSoldier()
What is wrong with my code now it says that I have an error but doesn’t show what it is?
# This level shows how to define your own functions.
# The code inside a function is not executed immediately. It's saved for later.
# This function has your hero collect the nearest coin.
def pickUpNearestCoin():
items = hero.findItems()
nearestCoin = hero.findNearest(items)
if nearestCoin:
hero.move(nearestCoin.pos)
# This function has your hero summon a soldier.
def summonSoldier():
# If hero.gold is greater than the cost of the "soldier":
if hero.gold > costOf("soldier"):
# Then summon a "soldier":
hero.summon("soldier")
pass
# This function commands your soldiers to attack their nearest enemy.
def commandSoldiers():
for soldier in hero.findFriends():
enemy = soldier.findNearestEnemy()
if enemy:
hero.command(soldier, "attack", enemy)
while True:
# In your loop, you can "call" the functions defined above.
# The following line causes the code inside the "pickUpNearestCoin" function to be executed.
pickUpNearestCoin()
# Call summonSoldier here
hero.summon("soldier")
# Call commandSoldiers here
hero.command(soldier, "attack", enemy)
def summonSoldier():
# If hero.gold is greater than the cost of the “soldier”:
if hero.gold > costOf(“soldier”):
# Then summon a “soldier”:
hero.summon(“soldier”)
pass
This function commands your soldiers to attack their nearest enemy.
def commandSoldiers():
for soldier in hero.findFriends():
enemy = soldier.findNearestEnemy()
if enemy:
hero.command(soldier, “attack”, enemy)
while True:
# In your loop, you can “call” the functions defined above.
# The following line causes the code inside the “pickUpNearestCoin” function to be executed.
pickUpNearestCoin()
# Call summonSoldier here
hero.summon(“soldier”)
# Call commandSoldiers here
hero.command(soldier, “attack”, enemy)
Hello @wazi, @guy_oy has not been active for a while, so he/she may not see your message.
If that is the correct code, and it solves, please remove it.
I suggest you make a new topic on this level.
Lydia
# This level shows how to define your own functions.
# The code inside a function is not executed immediately. It's saved for later.
# This function has your hero collect the nearest coin.
def pickUpNearestCoin():
items = hero.findItems()
nearestCoin = hero.findNearest(items)
if nearestCoin:
hero.move(nearestCoin.pos)
# This function has your hero summon a soldier.
def summonSoldier():
# Fill in code here to summon a soldier if you have enough gold.
if hero.gold == enoughGold:
hero.summon("soldier")
pass
# This function commands your soldiers to attack their nearest enemy.
def commandSoldiers():
for soldier in hero.findFriends():
enemy = soldier.findNearestEnemy()
if enemy:
hero.command(soldier, "attack", enemy)
while True:
# In your loop, you can "call" the functions defined above.
# The following line causes the code inside the "pickUpNearestCoin" function to be executed.
pickUpNearestCoin()
# Call summonSoldier here
summonSoldier()
# Call commandSoldiers here
commandSoldier()