在循环中以秒为单位显示时间

2024-04-26 18:49:45 发布

您现在位置:Python中文网/ 问答频道 /正文

我想创建一个while循环,以秒为单位打印时间。问题似乎是秒的值不断地重复打印相同的值。该值是循环执行时的值。我基本上希望循环不使用sec=sec+1来计算秒数。你知道吗

import time

t = time.gmtime()

def display_seconds(x):
    sec = time.strftime('%S',x)
    sec = int(sec)
    print sec

while True:
    display_seconds(t)

Tags: importtruetimedefdisplay时间单位sec
1条回答
网友
1楼 · 发布于 2024-04-26 18:49:45

所发生的事情是你只得到一次时间,然后一遍又一遍地显示相同的时间——注意t在你的循环中从不改变。试试这个:

import time

def display_seconds(x):
    sec = time.strftime('%S',x)
    sec = int(sec)
    print sec

while True:
    t = time.gmtime()
    display_seconds(t)

相关问题 更多 >