当每5秒执行我的程序时,我的time.ctime()不会改变。
我怎么才能修好它?
我的节目:
import random
import time
n = 1
future = time.time() + 600
for x in range(5): # program execute 5 times
print(n)
print time.ctime(future)
sensor1 = {'ID': 'A', 'float1': ['A' + str(i) for i in range(1, 17)]}
print(sensor1)
count = 16 # represents 16 float readings
for i in range(0, count):
r = random.random() # generates random real number between 0 and 1
print(r)
sensor2 = {'ID': 'B', 'float1': ['B' + str(i) for i in range(1, 17)]}
print(sensor2)
count = 16 # represents 16 float readings
for i in range(0, count):
r = random.random() # generates random real number between 0 and 1
print(r)
time.sleep(5) # to wait a second
n = n + 1发布于 2018-02-07 12:43:05
您的问题是,您设置了一个固定值的未来,并且您正在一次又一次地使用相同的固定值。
您可以使用以下方法打印当前时间:
print time.ctime()您可以使用以下方法打印当前时间+ 600:
print time.ctime(time.time() + 600)python时间手册指定:
time.ctime([secs])将时间转换为表示本地时间的字符串,时间以秒为单位。如果没有提供secs或None,则使用time()返回的当前时间。ctime(secs)等同于asctime(localtime(secs))。ctime()不使用区域设置信息。
示例代码(例如保存为time_prints.py):
import time
for x in range(5): # program execute 5 times
print time.ctime()
time.sleep(5)示例输出:
$ python time_prints.py
Thu Feb 8 14:37:41 2018
Thu Feb 8 14:37:46 2018
Thu Feb 8 14:37:51 2018
Thu Feb 8 14:37:56 2018
Thu Feb 8 14:38:01 2018https://askubuntu.com/questions/1003878
复制相似问题