首页 > 解决方案 > 在python中以10秒的间隔打印当前时间10次

问题描述

这会打印当前时间,间隔 10 秒我必须使用什么函数或循环

import time;
    
localtime = time.asctime(time.localtime(time.time()))
print "Local current time :", localtime

标签: pythondatetime

解决方案


你应该更好地论证这个问题!

顺便说一句,如果你试图每 10 秒打印一次当前时间 10 次,你可以这样做:

import time
from datetime import datetime


#For 10 times
for x in range(10):
  # Get current time
  now = datetime.now()
  # Make a string of it
  current_time = now.strftime("%H:%M:%S")
  # Print it
  print(current_time)

  # Wait for 10 seconds
  time.sleep(10)

输出:

14:33:33
14:33:43
14:33:53
14:34:03
14:34:13
14:34:23
14:34:33
14:34:43
14:34:53
14:35:03

推荐阅读