1

我目前正在运行一个程序,该程序有一个机器人移动成堆的“蜂鸣器”。

我希望能够在运行该函数的同时从函数返回一个值。当我运行该功能时,它会停止程序。我正在使用一个名为 pyKarel 的模块,它创建了一个机器人和蜂鸣器。count_beepers_1_1 和 count_beepers_2_1 是全局变量,count_beepers_1 和 count_beepers_2 是在shiftPiles方法中定义的局部变量。我想将局部变量保存到全局变量中。这是我的代码


def shiftPiles(arg, count_beepers_1, count_beepers_2):
   #shift the piles of beepers
   return count_beepers_1, count_beepers_2


while piles > 0:
   count_beepers_1_1, count_beepers_2_2 = shiftPiles(bob, count_beepers_1_1, count_beepers_2_1)

4

1 回答 1

2

您已经很好地定义了问题:您希望同时执行两块代码,而函数调用不会这样做。

这是多处理的经典应用程序。Python 有支持多进程、多线程的包(如果你确定你也可以使用它)。

还要看看使用生成器的可能性——一个在每次调用时产生一个值的“函数”,但每次连续调用都从最后一个调用停止的地方开始。

于 2019-09-13T18:35:11.247 回答