为了使你的脚本等待,使用time.sleep(num_seconds)
。 time.time()
只是返回当前时间;它根本没有做任何等待。
可能有一些代码在睡觉,但有其他代码在同一时间运行,做其他事情。要做到这一点,你必须使用threads,但需要一些时间才能习惯。也许this tutorial会有用。
编辑:哦也可以做到这一点没有线程,但你必须仔细跟踪你的变量。你搞砸了你的数学。说time.time()是1000时,你的代码就运行了。然后TimeReturned
为1005.假设用户输入Yes
需要1秒。然后if TimeReturned > time.time()
检查if 1005 > 1001
,这是真的。你真正想要检查的是if time.time() > TimeReturned
- 如果当前时间晚于TimeReturned
。
此外,您的脚本不具有互动性,所以很难看到任何进展。尝试运行此脚本:
import time
survivors = 15
survivor_return_seconds = 10.0
time_survivors_left = None
while True:
action = raw_input("Type 'x' to make survivors leave, ENTER to see how many are left: ")
#check if survivors returned
if time_survivors_left is not None:
if time.time() >= time_survivors_left + survivor_return_seconds:
survivors += 5
time_survivors_left = None
print "Survivors came back!"
if action == 'x':
if time_survivors_left is not None:
print "Survivors already left! Wait a bit!"
else:
survivors -= 5
time_survivors_left = time.time()
print "There are %s survivors left." % (survivors,)
if time_survivors_left is not None:
print "5 survivors will return in %.2fs" % (
time_survivors_left + survivor_return_seconds - time.time())
输出示例:
Type 'x' to make survivors leave, ENTER to see how many are left:
There are 15 survivors left.
Type 'x' to make survivors leave, ENTER to see how many are left:
There are 15 survivors left.
Type 'x' to make survivors leave, ENTER to see how many are left: x
There are 10 survivors left.
5 survivors will return in 9.99s
Type 'x' to make survivors leave, ENTER to see how many are left:
There are 10 survivors left.
5 survivors will return in 9.05s
Type 'x' to make survivors leave, ENTER to see how many are left:
There are 10 survivors left.
5 survivors will return in 7.66s
Type 'x' to make survivors leave, ENTER to see how many are left:
There are 10 survivors left.
5 survivors will return in 6.45s
Type 'x' to make survivors leave, ENTER to see how many are left: x
Survivors already left! Wait a bit!
There are 10 survivors left.
5 survivors will return in 5.73s
Type 'x' to make survivors leave, ENTER to see how many are left:
There are 10 survivors left.
5 survivors will return in 4.15s
Type 'x' to make survivors leave, ENTER to see how many are left:
There are 10 survivors left.
5 survivors will return in 2.90s
Type 'x' to make survivors leave, ENTER to see how many are left:
There are 10 survivors left.
5 survivors will return in 1.72s
Type 'x' to make survivors leave, ENTER to see how many are left:
There are 10 survivors left.
5 survivors will return in 0.48s
Type 'x' to make survivors leave, ENTER to see how many are left:
Survivors came back!
There are 15 survivors left.
Type 'x' to make survivors leave, ENTER to see how many are left:
There are 15 survivors left.
Type 'x' to make survivors leave, ENTER to see how many are left:
好,但不会让整个脚本停止(num_seconds)?我计划让幸存者减少大约20 * 60秒的时间,这样使脚本本身停止这么久会杀死我。如果可能的话,id可以在time.sleep(20 * 60)运行时做其他事情。 – BlueLance 2013-03-12 15:08:06
@ user2146277:会的。你可以不睡觉,看看我的更新。你刚刚得到了数学后退 – Claudiu 2013-03-12 17:06:19
该脚本的作品奇迹,我也阅读了教程和其他一些人,并了解更多线程中发生了什么。 为了让它能够同时发送多个幸存者的浪潮,是否有可能,或者最好在线程中完成? 感谢您的全力帮助:) – BlueLance 2013-03-12 18:40:09