Play Home Contribute Github Join Us! Discourse Staff Members Github Contribution Guides Team

[SOLVED] Python Blind Distance

Take the quotes off enemyDistance.

2 Likes

@MunkeyShynes I figured out thank you so much I literally could not have done this without you.
once again thanks!

1 Like

is some what worked for me but i died here is the imaje

All default comments deleted - your code without changes:

def nearestEnemyDistance():
    enemy = hero.findNearestEnemy()
    result = 10 # you set the default distance to enemy to 10
    if enemy:  # and if there is no enemy
        result = hero.distanceTo(enemy)
    return result # the distance to enemy will be 10
                  # if there is no enemy
while True:
    enemyDistance = nearestEnemyDistance()
    if enemyDistance > 0: # this will be always true
                          # 10 or the real distance
        hero.say(enemyDistance)

Question 1: is this line really needed?

result = 10 # or some other value

Can we comment it?
Question 2: Can we write only?:

 if enemyDistance: # without comparing enemyDistance to 0?

i get it now thank you

that actually helped me alot thank you it took me 2 days to figure it out thanks for the advice
:+1: :+1:

for some reason my wizard continuously attacks

here is my code`

type or paste code here
```def nearestEnemyDistance():
    enemy = hero.findNearestEnemy()
    result = 10
    if enemy:
        result = hero.distanceTo(enemy)
    return result

while True:
    # Call nearestEnemyDistance() and save the result in the variable enemyDistance
    enemyDistance = nearestEnemyDistance()
    # If the enemyDistance is greater than 0: 
    if enemyDistance > 0 : 
        # Say the value of enemyDistance variable.
        hero.say(enemyDistance)

@dedreous @ducky @CodingGeek14

def nearestEnemyDistance():
    enemy = hero.findNearestEnemy()
    result = 10
    if enemy:
        result = hero.distanceTo(enemy)
    return result

while True:
    # Call nearestEnemyDistance() and save the result in the variable enemyDistance
    enemyDistance = nearestEnemyDistance()
    # If the enemyDistance is greater than 0: 
    if enemyDistance > 0 : 
        # Say the value of enemyDistance variable.
        hero.say("enemyDistance")

The problem is with line 3 and 14. As written, line 3 will make enemyDIstance always > 0. What happens if there is no enemy…what is the value of result then?

Line 14, as written, is having the hero say a literal string value (which will always be “enemyDistance”), not the value of the variable…lose the quotation marks.

okay and could we talk in a dm

hi i am stuck i need help @Chaboi_3000 @Chaboi @anonymous
def nearestEnemyDistance():
enemy = hero.findNearestEnemy()
result = 10 # you set the default distance to enemy to 10
if enemy: # and if there is no enemy
result = hero.distanceTo(enemy)
return result # the distance to enemy will be 10
# if there is no enemy
while True:
enemyDistance = nearestEnemyDistance()
if enemyDistance > 0: # this will be always true
# 10 or the real distance
hero.say(enemyDistance)

Why do you think the wizard tires out?..he’s casting on every iteration of the loop. There are times when you don’t want him to case, therefore saving energy.

def nearestEnemyDistance():
    enemy = hero.findNearestEnemy()
    result = 10
    if enemy:
        result = hero.distanceTo(enemy)
    return result

while True:
    # Call nearestEnemyDistance() and save the result in the variable enemyDistance
    enemyDistance = nearestEnemyDistance()
    # If the enemyDistance is greater than 0: 
    if enemyDistance > 0 : 
        # Say the value of enemyDistance variable.
        hero.say(enemyDistance)
        enemy = hero.findNearestEnemy()
    if enemy:
        ready = hero.isReady("cleave")

can you please help me i cant move on if i cannot get past this level!!![quote=“Innerdesert4346, post:1, topic:11338, full:true”]
I’m trying to complete blind distance but i always run out of time because the wizard gets too tired too fast because he needlessly attacks when he doesn’t need too so i fail i have tried using stuff that isn’t involved in the level but no matter what it fails over and over its frustrating.
[/quote]

Zax, once again, it is your line 3 that is causing the problem…I see that you fixed the ‘hero.say’ statement :slight_smile: I copied your original code and ran it…yes, the wizard got tired. I changed line 3, as I had recommended, and passed the level.

Also, you’ve also added a new if statement…you don’t need this; you don’t want your hero to attack.

thx
(20 characters)
:wave: :wave:

type or paste code here
```# Tell the wizard the distance to the coming ogres.
hero.say("0")
# This function finds the nearest enemy and returns the distance to it.
# If there is no enemy, the function returns 0.
def nearestEnemyDistance():
    enemy = hero.findNearestEnemy()
    result = 0
    if enemy:
        result = hero.distanceTo(enemy)
    return result

while True:
    # Call nearestEnemyDistance() and
    # save the result in the variable enemyDistance.
    enemyDistance = nearestEnemyDistance()
    # If the enemyDistance is greater than 0: 
    enemy = hero.findNearestEnemy()
    if enemy:
        if hero.distanceTo(enemy) > 0:
            # Say the value of enemyDistance variable.
            nearestenemyDistance = hero.findNearestEnemy()
            hero.say("0")
I did everything to make this work. But it says that the code never finished.

Make sure you follow what the comments say:

And:

Danny

It says that the result is zero, and then it says that the distance to the enemy is zero. So, I programmed my hero to say zero. But then it stopped at hero.findNearestEnemy.

These lines aren’t quite right:

        if hero.distanceTo(enemy) > 0:
            # Say the value of enemyDistance variable.
            nearestenemyDistance = hero.findNearestEnemy()
            hero.say("0")
  1. You want the if statement to check that enemyDistance is greater than 0, not hero.distanceTo(enemy).
  2. You don’t need the line setting nearestenemyDistance at all.
  3. You want the hero to say the enemyDistance, not just say 0.

Try changing those, hopefully that helps things!

1 Like