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

Please Help (Clash of Clones- Python)

I’ve tried several ways and no matter how i try to fix my code my hero just continues to electrocute the enemy instead of bashing them.
Please help me, here is my current code:

while True:
    enemy = hero.findNearestEnemy()
    hero.shield()
    
    if hero.canElectrocute(enemy):
            hero.electrocute(enemy)
    if enemy and enemy.type != "sand-yak":
        hero.shield()
        if hero.distanceTo(enemy) < 10:
            hero.bash(enemy)
        

Hello,
CanElectrocute only checks if enemy can be electrocuted (distance, type etc.), but you also need to check if you are ready to electrocute (use hero.isReady()). You also need to check if you can bash.

Hi Princess_Fayette. Welcome to Discourse. Your syntax is okay but the main issue I see is the structure of your code. Let’s take it line by line.

while True:
    enemy = hero.findNearestEnemy()  #  yes, we need this first.
    hero.shield()  # This is saying to shield for no apparent reason.  Just shield.
                 #  Probably not a good idea.  Shielding is best when combined with a conditional for circumstances to shield.
    if hero.canElectrocute(enemy):  #  What if there's no enemy?  Code will fail.  Always check if there's
            hero.electrocute(enemy)  # an enemy first, before telling your hero to interact with the enemy.
    if enemy and enemy.type != "sand-yak":  # This should be placed immediately after defining enemy
        hero.shield()                       # and before any attack code.
        if hero.distanceTo(enemy) < 10:  #  This won't run at all.  It would work better as an else statement
            hero.bash(enemy)             # so that it runs during the electrocute cooldown.
        

Try switching things around, use elif and else conditionals, and it should work.
Define enemy.
Check for enemy.
Electrocute enemy.
Bash enemy.
Shield.