Python 在设定的时间内减少变量
我试图将一个变量减少5,打印新变量,然后在X时间之后再次增加变量并打印变量。这是一个基于文本的小游戏,你把一些人打发走,他们在一定时间内不会回来Python 在设定的时间内减少变量,python,python-2.7,Python,Python 2.7,我试图将一个变量减少5,打印新变量,然后在X时间之后再次增加变量并打印变量。这是一个基于文本的小游戏,你把一些人打发走,他们在一定时间内不会回来 import time TimeReturned = time.time() + 5 Survivors = 10 Test = raw_input("Run Script?") if Test == "Yes": print Survivors if TimeReturned > time.time(): S
import time
TimeReturned = time.time() + 5
Survivors = 10
Test = raw_input("Run Script?")
if Test == "Yes":
print Survivors
if TimeReturned > time.time():
Survivors -= 5
print Survivors
Survivors += 5
print Survivors
当输出为10,5,10时,我得到的结果是,它根本不会延迟时间。这是我的第一个问题,很抱歉,如果它是模糊的
感谢您的耐心:)要让脚本等待,请使用。只返回当前时间;这根本不会让任何事情等待 可以让一些代码处于休眠状态,同时让其他代码运行,执行其他操作。要做到这一点,你必须使用,虽然,这需要一些时间来适应。也许会有用 编辑:哦,没有线程也可以这样做,但是你必须仔细跟踪你的变量。你的数学有点搞砸了。假设time.time()为1000时,代码运行。然后
TimeReturned
为1005。假设用户花了1秒时间键入Yes
。然后,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:
嗯。。。你没有做任何事情让脚本停止并等待
if TimeReturned > time.time():
这只是检查条件是否正确并相应地进行处理,它不会指示任何人等待
您应该使用time.sleep(secs)
函数在所需的时间内停止代码的执行
import time
TimeReturned = time.time() + 5
Survivors = 10
Test = raw_input("Run Script?")
if Test == "Yes":
print Survivors
if TimeReturned > time.time():
Survivors -= 5
print Survivors
Survivors += 5
print Survivors
但是请注意,在睡眠时间内,不会执行任何操作。因此,如果您希望一些后台进程继续运行,并且只希望您提到的那些人等待,那么您应该在它自己的线程中运行脚本的这一部分。您可以使用
计时器执行您想要的操作。它运行指定的秒数,然后执行指定的函数。Timer
只是使用第二个线程的一种简单方法,这就是为什么它位于threading
模块中
from threading import Timer
import time
Survivors = 10
def increase_survivors():
global Survivors
Survivors += 5
# start execution
print Survivors
Survivors -= 5
t = Timer(5, increase_survivors)
t.start()
for i in range(10):
print Survivors
time.sleep(1)
最后一点只是展示它是如何工作的。也许有一种更好的方法来管理变量,而不是那种global
语句-我肯定会得到一些建议。好的,但这不会让整个脚本停止(num_秒)吗?我计划让幸存者的时间减少大约20*60秒,所以让脚本本身停止那么长时间会让我丧命。如果可能的话,我希望它能够在运行time.sleep(20*60)的同时做其他事情。@user2146277:是的。你可以不用睡觉,看看我的更新。你刚刚得到了数学上的退步,这个脚本非常棒,我也阅读了教程和其他一些教程,并了解了线程中发生的更多事情。为了让我可以同时发送多个幸存者波,这是可能的,还是最好在线程中完成?感谢您的帮助:)@user2146277:如果您认为在用户输入内容之前不会发生任何事情,那么您可以使用raw\u input
处理所有事件。但这意味着,如果必须在5秒钟内发生某些事情,并且用户需要3分钟来输入某些内容,那么直到那时他们才会看到。如果你想让事情同时发生,那么你必须使用线程非常感谢你的帮助:)我将尝试使用原始输入,看看它是否有效,如果无效,那么我将使用线程:)谢谢,幸运的是我不知道如何创建线程(至少我不知道)我是Python和编程新手,我决定如果我开始了一个项目,我将能够在构建它的同时学习到所需的东西,到目前为止,它已经工作了,但尝试做这部分只会带来问题。我会查一下:)谢谢:)克劳迪乌给我提供了一个线程链接,这个链接应该会有帮助:)计时器运行5秒后,会不会增加幸存者5,使幸存者=15?我错过了你从减少幸存者开始的第一点。不管怎样,重要的部分是在改变变量之前等待,不是吗?