Vital powers (Confused)

here is my code.

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()

It keeps on saying this

i’ll just reset the level but what was wrong with my code?

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?

never mind finished the level

hi is there a problem with my code

# 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)

hi @guy_oy is there a problem with my code

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)

I’m just trying to get the bonuses

please format your code correctly above^ @wazi

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

I agree with @Lydia_Song because @guy_oy was last active on this topic about a year ago so ya

1 Like

Yeah. Maybe you should do
hero.command(soldier.attack(enemy))

i need help

# 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()

type or paste code here

this my gear actually i have the obsidian sheiild but i forgot to equip it

The “s” isn’t needed here because it isn’t defined this way. The problem here is this line:

enoughGold isn’t defined, instead, you need to find the cost of the soldier (in the methods tab) and put the value instead.

2 Likes

the thing is i solved it right after :rofl: :rofl:

2 Likes