What does it mean that a variable equals none?

I’m starting to see this kind of variables that equal None:

def findStrongestTarget():
    mostHealth = 0
    bestTarget = None
    enemies = self.findEnemies()

But I don’t understand well yet what it means. Does it mean that the variable is empty? Why can’t we just use variable = 0 like the mostHealth variable?

  1. None is a special value and:
bestTargert=None

means that bestTarget does not have any usable value,

If you do not find a target, then further on in your code the conditional

if (bestTarget)
    attack(bestTarget)

will fail.

Shortly any value that evaluates to false in if(value) can be used

Didn’t understand that last sentence very well, may you explain it with an example?

Very often you need to set a convention that some values are bad . Take this code:

 attack(enemy)

What happens if there is no enemy? You need to account for this. So you decide to do not attack the enemy if the value of the enemy is equivalent to false

if( enemy)
    attack(enemy)

Opps, now we talk about false. What it means?

if is a test. The expression inside the if can also be either true or false.

If expression inside the if is not a number we need a convention that tell us if a value is false or true.
The convention is:
Every non-zero value is True
Every 0 value or None or undefined variable is false

1 Like

omg I’m having a hard time here. I did Library Tactician level and this line that says bestTarget = None was default in it:

def findStrongestTarget():
    mostHealth = 0
    bestTarget = None



    enemies = self.findEnemies()
    # Figure out which enemy has the most health, and set bestTarget to be that enemy.
    for enemy in enemies:
        if enemy.health>mostHealth:
            bestTarget=enemy
    # Only focus archers' fire if there is a big ogre.
    if bestTarget and bestTarget.health > 15:
        return bestTarget
    else:
        return None

After my code worked I erased that line and everything worked just fine. Then is it important to even know how this None works?

Later on, you’ll get a value not defined error and you’ll not understand why.

If there is not a single enemy on the screen, the code will fail.
It just happens that for this level you always have something to kill.