def inAttackRange(enemy):
distance = hero.distanceTo(enemy)
# Almost all swords have attack range of 3.
if hero.distanceTo(enemy) <= 3:
return True
else:
return False
while True:
# Find the nearest enemy and store it in a variable.
enemy = hero.findNearestEnemy()
# Call inAttackRange(enemy), with the enemy as the argument
# and save the result in the variable canAttack.
inAttackRange(enemy);
# If the result stored in canAttack is True, then attack!
hero.attack(enemy)
pass
I noticed your spacing is off. Is that just how it copied?
In the lines
def inAttackRange(enemy):
distance = hero.distanceTo(enemy)
# Almost all swords have attack range of 3.
if hero.distanceTo(enemy) <= 3:
return True
else:
return False
you already defined hero.distanceTo(enemy) as distance. Use the variable instead.
use the variable canAttack in your while loop to save the function created and then use an if to see if the function is true. if it is true, then attack the enemy.
Not that it matters for functioning code, the same goes with the final ‘pass’ statement. Sure it is just a placeholder, but it is best practice to place it correctly so that you don’t get in to the habit and forget in future code.
is wrong and I don’t know what to do
it says argument error for line four.
def inAttackRange(enemy):
distance = hero.distanceTo(enemy)
# Almost all swords have attack range of 3.
if hero.distanceTo(enemy) is <= 3:
return True
else:
return False
while True:
# Find the nearest enemy and store it in a variable.
enemy = hero.findNearestEnemy()
# Call inAttackRange(enemy), with the enemy as the argument
enemy = canAttack
# and save the result in the variable canAttack.
inAttackRange(enemy);
# If the result stored in canAttack is True, then attack!
hero.attack(enemy)
pass
my new code is also above so you can check it out. if anything else is wrong let me know!
make a new variabile called canAttack and give it the boolean value that inAttackRange(enemy) returns
check if what is stored in canAttack is True
attack the enemy
def inAttackRange(enemy):
distance = hero.distanceTo(enemy)
# Almost all swords have attack range of 3.
if hero.distanceTo(enemy) is <= 3:
return True
else:
return False
while True:
# Find the nearest enemy and store it in a variable.
enemy = hero.findNearestEnemy()
# Call inAttackRange(enemy), with the enemy as the argument
enemy = canAttack
# and save the result in the variable canAttack.
inAttackRange(enemy);
# If the result stored in canAttack is True, then attack!
hero.attack(enemy)
pass