Distraction Maneuver Help


#1

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)


#2

it says to reassign furthestUnit and maxDistance.


#3

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.


#4

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)


#5

reassign the maxDistance and furthestUnit into a different statement


#6

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.


#7

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


#8
unitDistance = currentUnit.distance

currentUnit.distance is not a valid method. Fix this one line and it will complete successfully.


#9

I have no idea how tho


#10

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.