I don’t understand how to reassign furthestUnit and maxDistance, please help
# Protect the peasants!
# This function find the furthest one from units.
def findFurthest(units):
furthestUnit = None
maxDistance = 0
unitIndex = 0
while unitIndex < len(units):
currentUnit = units[unitIndex]
# Find the distance to currentUnit:
unitDistance = currentUnit.distance
# If that distance greater than maxDistance:
if unitDistance > maxDistance:
# Reassign furthestUnit and maxDistance:
unitIndex += 1
return furthestUnit
# It's like findNearestEnemy but vice versus.
def findFurthestEnemy():
enemies = hero.findEnemies()
furthestEnemy = findFurthest(enemies)
# Return furthestEnemy:
return furthestEnemy
# The function makes the hero attack without distractions.
def attackWhileAlive(target):
# Attack target while it's health > 0:
while (target).health > 0:
hero.attack(target)
pass
while True:
# To protect peasants, hunt for furthest ogres.
furthestOgre = findFurthestEnemy()
if furthestOgre:
attackWhileAlive(furthestOgre)
it says to reassign furthestUnit and maxDistance.
You’re very close. Like Seojin says, follow the directions.
if unitDistance > maxDistance:
furthestUnit = ?
maxDistance = ?
You have already defined the variables to use in the assignment. Just fill them in where they belong.
I tried this, but now line 11 has error code that says “TypeError: Can’t read protected property: distance”
# Protect the peasants!
# This function find the furthest one from units.
def findFurthest(units):
furthestUnit = None
maxDistance = 0
unitIndex = 0
while unitIndex < len(units):
currentUnit = units[unitIndex]
# Find the distance to currentUnit:
unitDistance = currentUnit.distance
# If that distance greater than maxDistance:
if unitDistance > maxDistance:
# Reassign furthestUnit and maxDistance:
furthestUnit = None
maxDistance = 0
unitIndex += 1
return furthestUnit
# It's like findNearestEnemy but vice versus.
def findFurthestEnemy():
enemies = hero.findEnemies()
furthestEnemy = findFurthest(enemies)
# Return furthestEnemy:
return furthestEnemy
# The function makes the hero attack without distractions.
def attackWhileAlive(target):
# Attack target while it's health > 0:
while (target).health > 0:
hero.attack(target)
pass
while True:
# To protect peasants, hunt for furthest ogres.
furthestOgre = findFurthestEnemy()
if furthestOgre:
attackWhileAlive(furthestOgre)
reassign the maxDistance and furthestUnit into a different statement
You didn’t reassign the variables. You restated their original assignment. You need to reassign the variables to the variables directly above them in the while loop.
so this?
# Protect the peasants!
# This function find the furthest one from units.
def findFurthest(units):
furthestUnit = None
maxDistance = 0
unitIndex = 0
while unitIndex < len(units):
currentUnit = units[unitIndex]
# Find the distance to currentUnit:
unitDistance = currentUnit.distance
# If that distance greater than maxDistance:
if unitDistance > maxDistance:
# Reassign furthestUnit and maxDistance:
furthestUnit = currentUnit
maxDistance = unitDistance
unitIndex += 1
return furthestUnit
# It's like findNearestEnemy but vice versus.
def findFurthestEnemy():
enemies = hero.findEnemies()
furthestEnemy = findFurthest(enemies)
# Return furthestEnemy:
return furthestEnemy
# The function makes the hero attack without distractions.
def attackWhileAlive(target):
# Attack target while it's health > 0:
while (target).health > 0:
hero.attack(target)
pass
while True:
# To protect peasants, hunt for furthest ogres.
furthestOgre = findFurthestEnemy()
if furthestOgre:
attackWhileAlive(furthestOgre)
still has the error message though
unitDistance = currentUnit.distance
currentUnit.distance
is not a valid method. Fix this one line and it will complete successfully.
You have to assign your variable, unitDistance
, a valid method.
unitDistance = someone’s distance to (argument)
Your current assignment is on the right track, it’s just not a valid method. Look through your methods and you’ll find the right one.