爲了使你的腳本等待,使用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