Play Home Contribute Github Join Us! Discourse Staff Members Github Contribution Guides Team

Return statement TROUBLE

Hi, I am struggling witha return statement.
Here is my code.
Can someone tell me what i did wrong? My hero doesnt have an error, but isnt summoning.
Code Here.


summonTypes = ['soldier', 'soldier', 'archer', 'decoy']
def summon():
    type = summonTypes[len(hero.built) % len(summonTypes)]
    return type
def commandPeasant():
    friends = hero.findFriends()
    for friend in friends:
        if friend.type == 'peasant':
            if (summon == 'decoy') and hero.gold > 25:
                hero.command(friend, "buildXY", 'decoy', friend.pos.x, friend.pos.y)
            else:
                
                items = hero.findItems()
                item = friend.findNearest(items)
                if item:
                    hero.command(friend, "move", item.pos)
def commandSoldier():
    friends = hero.findFriends()
    for friend in friends:
        if friend.type == 'archer':
            enemies = hero.findEnemies()
            target = friend.findNearest(enemies)
            if target:
                hero.command(friend, "attack", target)
def commandArcher():
    friends = hero.findFriends()
    for friend in friends:
        if friend.type == 'soldier':
            targets = hero.findByType("archer", hero.findEnemies())
            target = friend.findNearest(targets)
            if target:
                hero.command(friend, "attack", target)
            else:
                enemies = hero.findEnemies()
                enemy = friend.findNearest(enemies)
                if enemy:
                    hero.command(friend, "attack", enemy)
def T():
    if (summon == 'soldier') and hero.gold > 20:
        hero.summon("soldier")
    if (summon == 'archer') and hero.gold > 25:
        hero.summon('archer')
def attack():
    item = hero.findNearestItem()
    if item:
        hero.move(item.pos)
while True:
    attack()
    commandSoldier()
    commandPeasant()
    commandArcher()
    T()
    summon()

Is this level steelClaw Gap?

1 Like

No. It is a custom level

I don’t think I can help with that, but others may be able to help you if you send
the link.

if (summon == 'soldier')

I believe you’re trying to call the function summon, in which case you need the parenthesis.

if (summon() == 'soldier')

Otherwise you are comparing the function summon to the string 'soldier', which of course is never true.


The function T (which needs a more descriptive name, by the way) can be rewritten more efficiently by reusing the return value of summon:

def summonUnit():
    unitType = summon()
    if hero.gold > hero.costOf(unitType):
        hero.summon(unitType)
1 Like

Thank you so much!!!

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