Python 尝试在设定的时间内打印变量

Python 尝试在设定的时间内打印变量,python,Python,程序运行时无法获取要更新的“当前”unix时间戳 import time current = time.time() dura = current + 1 count = 1 while current <= dura: while count == 1: print('hello') count = count + 1 导入时间 当前=时间。时间() 杜拉=电流+1 计数=1 虽然当前基于的评论,代码是: import time curren

程序运行时无法获取要更新的“当前”unix时间戳

import time

current = time.time()
dura = current + 1

count = 1
while current <= dura:
    while count == 1:
        print('hello')
        count = count + 1
导入时间
当前=时间。时间()
杜拉=电流+1
计数=1
虽然当前基于的评论,代码是:

import time
current = time.time()

dura = current + 1
count = 1
while current <= dura:
    current = time.time()
    while count == 1:
        print('hello')
        count = count + 1
导入时间
当前=时间。时间()
杜拉=电流+1
计数=1

当前时,您只使用一次
time.time()。如果您想稍后获取当前时间,您必须再次调用
time.time()
。您好,欢迎使用SO!你能解释一下你打算实现什么吗。因为一旦使用当前时间更新“current”,while循环的执行将受到影响。感谢此解决方案不包括将
time.time()
替换为
datetime.now()
或使用
time.sleep()
。它在循环中再次调用
datetime.now()
(或在原始代码
time.time()
)来解决问题。