所以我在这里有这个。
import sys
import os
import time
def clock():
Minutes = 0
Hours = 1
while True:
Minutes += 1
if Minutes == 60:
Minutes = 0
Hours = 2
if Hours == 12:
Hours = 1
Minutes = 0
break
ReadLine = ("\t{0:>2} : {1:>2}\r").format(Hours, Minutes)
sys.stdout.write(ReadLine)
sys.stdout.flush()
time.sleep(60)
clock()
仅仅为了记录,我已经确定压痕是正确的,它们在这里看起来有点棘手。我意识到我没有为A.M./P.M设置任何内容。到目前为止..任何帮助都赞赏这个菜鸟。 谢谢 - 马特。
编辑:
>>> 2: 0 2: 0 2: 0
这是现在正在打印的内容,会议记录尚未更新。我显然遗漏了一些东西。再次感谢您的帮助,如果这是重复,我很抱歉,我已经找到了答案,但没有找到答案。谢谢 - 马特。
编辑#2-我弄清楚了。我使用了两个答案,虽然我接受了它会很慢的事实,它会做我想要的事情。
import sys
import os
import time
def clock():
Minutes = 0
Hours = 1
AM_PM = "AM" if Hours < 12 else "P.M"
while True:
Minutes += 1
if Minutes == 60:
Minutes = 0
Hours += 1
if Hours == 24:
Hours = 1
Minutes = 0
break
ReadLine = ("\t{:>2} : {:>2} {}\r").format(Hours, Minutes, AM_PM)
sys.stdout.write(ReadLine)
sys.stdout.flush()
time.sleep(60)
clock()
你知道,似乎无论我怎么努力,我都无法让这个黯淡的缩进看起来正确。哦,好吧,我希望你能理解它只是向右边标记了一下。
答案 0 :(得分:1)
import datetime
then = now = datetime.datetime.now()
minutes = 0
hours = 0
while True:
now = datetime.datetime.now()
if (now-then).total_seconds() > 60:
then += datetime.timedelta(minutes=1)
minutes += 1
if minutes == 60:
minutes = 0
hours += 1
if hours == 24:
hours = 0
am_pm = "AM" if hours < 12 else "PM"
print("{:>2}:{:>02} {}".format((hours+11)%12+1, minutes, am_pm))
请注意,我不再使用time.sleep
,因为它无法保证在请求的时间内完全睡眠(实际上,您将始终按照该时钟稍微慢一点运行),而是将当前时间与上一分钟的时间进行比较传递并查看总秒数是否超过60.如果是,则递增分钟,如果分钟为60,则递增小时并检查翻转和am_pm切换。之后,打印时间。
如果你想稍微伸展双腿,可以尝试在课堂上练习!哦哦,还有穿线!
import datetime
import threading
import queue
class Clock(object):
def __init__(self, current_time=None):
if isinstance(current_time, datetime.datetime):
hours, minutes = current_time.hour, current_time.minute
else:
hours = minutes = 0
self.hours = hours
self.minutes = minutes
self.q = queue.Queue()
def checkTime(self):
try:
self.q.get_nowait() # time has updated, or exception thrown
self.updateTime()
self.q.task_done()
except queue.Empty:
pass # time hasn't updated
def updateTime(self, num_mins=1):
self.minutes += 1
if self.minutes == 60:
self.minutes = 0
self.hours += 1
if self.hours == 24:
self.hours = 0
print(self)
def minutePassed(self):
then = datetime.datetime.now()
while True:
now = datetime.datetime.now()
if (now-then).total_seconds() > 60:
then += datetime.timedelta(minutes=1)
self.q.put('_') # put something there, doesn't matter what
def __str__(self):
am_pm = "AM" if self.hours < 12 else "PM"
return "{:>2}:{:>02} {}".format((self.hours+11)%12+1,
self.minutes, am_pm)
def start(self):
t = threading.Thread(target=self.minutePassed)
t.daemon=True
t.start()
while True:
self.checkTime()
clock = Clock(datetime.datetime.now())
clock.start()
答案 1 :(得分:1)
您的代码没有打印任何内容,因为您将打印的代码放在if
语句中的stdout中。所以它只会在Minutes == 60
和Hours == 12
时打印(由于你没有按照评论中的含义增加Hours
而永远不会发生。
试试这个:
import sys
import os
import time
def clock():
Minutes = 0
Hours = 1
ampm = "AM"
while True:
Minutes += 1
if Minutes == 60:
Minutes = 0
Hours += 1
if Hours == 12:
Hours = 1
Minutes = 0
ampm = ("AM" if (ampm == "PM") else "PM")
ReadLine = ("\t{0:>2} : {1:>2} {2} \r").format(Hours, Minutes,ampm)
sys.stdout.write(ReadLine)
sys.stdout.flush()
time.sleep(60)
clock()