我正在做这教程。
基本上,我的问题是,在front_is_clear为false之后,它运行jump_over_hurdle并停止。
from my_lib import *
while front_is_clear():
move()
if not front_is_clear():
jump_over_hurdle()
我怎么才能让它回到过去
while front_is_clear():
move()
此外,我希望这个项目一旦达到目标就结束。所以我需要如何实现..。
if at_goal():
done()
发布于 2014-11-08 18:29:37
只需使用另一个while循环,如
from my_lib import *
# well, maybe not `not at_goal()` since it only check it after
# the below code finish running, it would be better to use `while True`
# and find a better way to implement the at_goal()
while not at_goal():
while front_is_clear():
move()
if not front_is_clear():
jump_over_hurdle()
done()
发布于 2014-11-08 18:55:39
没有必要使用嵌套循环。在原始代码中,while循环与if -check是冗余的;如果您重复执行move
while front_is_clear
,那么当然不会出现循环结束后的front_is_clear
(否则它会继续循环)的情况。
实际上,我们想要做的是反复移动或跳跃,直到我们达到目标:
while not at_goal():
if front_is_clear():
move()
else:
jump_over_hurdle()
done()
这也避免了在“移动到下一个障碍”过程中实现目标的原始代码中的问题,因为我们检查在每一步之后我们是否已经达到了目标(无论是移动还是跳跃)。
https://stackoverflow.com/questions/26824289
复制相似问题