codecombat吧 关注:1,811贴子:3,530
  • 1回复贴,共1

请教在全局定义变量和在while内定义变量有什么不同?

只看楼主收藏回复

请教在全局定义变量和在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)


IP属地:广东1楼2018-03-17 20:20回复
    请教大神,我觉得enemy变量在全局还是while内定义应该是一样的,但是结果不同,是游戏有问题还是我输入的有问题?


    IP属地:广东2楼2018-03-17 20:23
    回复