I would like to create a while loop which prints time in seconds. The problem seems to be that the value for seconds keeps printing the same value over and over. This value is when the loop is enacted. I basically want the loop to count seconds without using 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)
Run Code Online (Sandbox Code Playgroud)
发生的事情是你得到的时间一次,然后一遍又一遍地显示同一时间 - 注意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)
Run Code Online (Sandbox Code Playgroud)