减少变量一段时间

时间:2013-03-12 14:38:23

标签: 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():
        Survivors -= 5
        print Survivors
    Survivors += 5
    print Survivors

我得到的所有输出都是10,5,10直接输出,它根本不会延迟时间。这是我的第一个问题,如果它含糊不清就很抱歉。

  1. 我做错了什么导致此代码立即返回变量?

  2. 如果我想在脚本中执行其他操作,例如将剩余的Survivors发送出去也可以在此脚本运行时或者我需要等待5秒钟?

  3. 感谢您的耐心:)

3 个答案:

答案 0 :(得分:1)

为了让您的脚本等待,请使用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: 

答案 1 :(得分:0)

好吧......你没有做任何让脚本停止等待的事情。

if TimeReturned > time.time():

这只是检查条件是否为真并相应地进行,它不会指示任何人等待。

你应该使用time.sleep(secs)函数来停止执行代码所需的时间。

但请注意,在睡眠时间内,没有执行任何操作。所以,如果你想让一些后台进程继续运行,只有你提到的那些人等待,你应该在它自己的线程中运行该部分脚本。

答案 2 :(得分:0)

你可以用Timer做你想做的事。它运行指定的秒数,然后执行您指定的功能。 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更好 - 我相信我会得到一些建议。