help me with the hit and freeze this is the code i use
This function checks if the enemy is in your attack range.
def inAttackRange(enemy):
distance = hero.distanceTo(enemy)
# Almost all swords have attack range of 3.
if distance <= 3:
return True
else:
return False
Attack ogres only when they’re within reach.
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
When the code says to call a function and save it in a variable, it should look something like this:
exampleVariable = exampleFunction(parameter)
Then you use if exampleVariable = True: and do whatever you need to do. Use this format to correct the last few lines of your code, and you’re good to go.
Hope this helps
So in your code, the level tells you to call inAttackRange(enemy), right? Then it tells you to save the result in the variable canAttack.
Therefore if we replace the names of the variables, functions, and parameters:
exampleVariable = exampleFunction(parameter)
if exampleVariable = True:
# turns into...
canAttack = inAttackRange(enemy)
if canAttack = True:
It helps us help you if you format your code correctly. If you are going to ask for help on the forums please surround your code in triple backticks ( ` ) so we can see your indentation.
As for the help, there is a comment telling you what to do:
# Call inAttackRange(enemy), with the enemy as the argument
# and save the result in the variable canAttack.
You are currently calling inAttackRange(enemy) but you are not save the result in a variable
# This function checks if the enemy is in your attack range.
def inAttackRange(enemy):
distance = hero.distanceTo(enemy)
# Almost all swords have attack range of 3.
if distance <= 3:
return True
else:
return False
# Attack ogres only when they're within reach.
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.
# If the result stored in canAttack is True, then attack!
pass
I see you do not understand your mistake. Hopefully this will clarify.
Read the instructions inside the gray lines.
# Call inAttackRange(enemy), with the enemy as the argument # and save the result in the variable canAttack.
Use what you have learned in previous levels to solve this. First call in inAttackRange, as the gray line has said. The code for calling inAttackRange is right there. Then to the left of it, assign it to variable canAttack (something you learned in dungeon).
# If the result stored in canAttack is True, then attack!
After that, write an if statement saying if canAttack is true, attack.