This is my code. The character won’t move to the flag to pick it up.
# Help your friends beat the minions that Thoktar sends against you.
# You'll need great equipment and strategy to win.
# Flags might help, but it's up to you–be creative!
# There is a doctor behind the fence. Move to the X to get healed!
flag = hero.findflag("green")
enemy = hero.findNearestEnemy()
while True:
if flag:
hero.pickUpFlag(flag)
elif enemy:
if hero.isReady("cleave"):
hero.cleave(enemy)
else:
hero.attack(enemy)
pass
Your current code just looks for the flag once at the beginning. Move the flag variable line into the while True loop and it will always be checking for a flag. You’ll want to do the same with the enemy variable, move that into the while True to make sure your hero is always looking for one.
Okay, I’ll try that really quick.
Thank you @brooksy125, for your help it worked.
You are welcome! Glad to help.
what is wrong with this code :
while True:
flag = hero.findflag(“green”)
enemy = hero.findNearestEnemy()
if flag:
hero.pickUpFlag(flag)
elif enemy < 11:
if hero.isReady(“cleave”):
hero.cleave(enemy)
else:
hero.shield()
hero.attack(enemy)
pass
Well. First make sure hero.findFlag(“green”) has a capital F for Flag.
Then, you have to use hero.distanceTo(enemy) < 11, rather than just enemy < 11. Enemy is a person, not a number. You can only use < and > for numbers. It doesn’t work with people.
Also, what if the enemy is closer than 11, but you’re not ready to cleave? Maybe you should put an elif enemy inside that so that you can attack an enemy even when it’s closer than 11.
Finally, in your else, you attack an enemy without checking it exists. Maybe you should use elif enemy again.
Please could you put ``` on a new line before and after your code, so that I can see the spaces at the start of the line.
3 Likes