擅长:python、mysql、java
<p>尝试创建另一个线程来跟踪时间,然后更新全局布尔值:</p>
<pre><code>from threading import Thread
from time import sleep
timeIsUp = False
threadKill = False
def answerTime(self):
sleep(self)
if threadKill = True:
self._is_running = False
else:
timeIsUp = True
self._is_running = False
thread = Thread(target = answerTime, args = (10)
</code></pre>
<p>现在您可以让主代码在<code>while</code>循环中有一个附加语句:</p>
<pre><code>while path != "e" and path !="n" and timeIsUp==False:
...
if path =="e":
station()
threadKill=True
elif path =="n":
estate()
threadKill=True
elif path =="s":
building()
threadKill=True
else:
print("Time is up")
</code></pre>