请教在全局定义变量和在while内定义变量有什么不同?
def shouldRun():
if hero.health < hero.maxHealth / 2:
return True
else:
return False
enemy = hero.findNearestEnemy() //在全局定义,hero到半血不会跑
while True:
# Move to the X only if shouldRun() returns True
if shouldRun():
hero.moveXY(75, 37)
# Else, attack!
else:
hero.attack(enemy)
def shouldRun():
if hero.health < hero.maxHealth / 2:
return True
else:
return False
while True:
enemy = hero.findNearestEnemy() //在while下定义,hero在半血时会跑
# Move to the X only if shouldRun() returns True
if shouldRun():
hero.moveXY(75, 37)
# Else, attack!
else:
hero.attack(enemy)
def shouldRun():
if hero.health < hero.maxHealth / 2:
return True
else:
return False
enemy = hero.findNearestEnemy() //在全局定义,hero到半血不会跑
while True:
# Move to the X only if shouldRun() returns True
if shouldRun():
hero.moveXY(75, 37)
# Else, attack!
else:
hero.attack(enemy)
def shouldRun():
if hero.health < hero.maxHealth / 2:
return True
else:
return False
while True:
enemy = hero.findNearestEnemy() //在while下定义,hero在半血时会跑
# Move to the X only if shouldRun() returns True
if shouldRun():
hero.moveXY(75, 37)
# Else, attack!
else:
hero.attack(enemy)