如何创建一个返回军事时间的课程

我创建了一个可以正确返回hh:mm时间格式的代码,但是当我在另一个文件中使用该代码时,与预期的输出相比,输出是不正确的。

例如:

它应该返回以下内容: 我们在05:00醒来 我们在05:30起床 我们又睡了06:10 最后电车于23:00出发 今天结束于23:59 明天从00:00开始

但是我的回报: 我们在05:00醒来 我们在05:30起床 我们又睡了06:10 最后电车于23:00出发 今天结束于23:59 明天开始于24:00

第24小时应变为00:00,我不确定如何解决。

class MilClock:
    def __init__(self,hours,minutes):
        self.hours = int(hours)
        self.minutes = int(minutes)
    def __str__(self):
        return f"{self.hours:02}:{self.minutes:02}"
    def addOne(self):
        self.minutes += 1
        if self.minutes >= 60:
            self.minutes = 0
            self.hours += 1

'''下一个文件,其中包含''

上方的代码
from milclock import *
def addMinutes(clock,n):
    for x in range(n):
        clock.addOne()
hallClock = MilClock(5,0)
print('We wake up at',hallClock)
addMinutes(hallClock,30)
print('We get up at',40)
print('We are sleepy again by',hallClock)



wristWatch = MilClock(23,0)
print('Last tram leaves at',wristWatch)
addMinutes(wristWatch,59)
print('Today ends at',wristWatch)
wristWatch.addOne()
print('Tomorrow starts at',wristWatch)
aabb5888157 回答:如何创建一个返回军事时间的课程

在您对addOne()的定义中,尝试为小时数附加一个类似的测试,与分钟数相同:

def addOne(self):
    self.minutes += 1
    if self.minutes >= 60:
        self.minutes = 0
        self.hours += 1
        if self.hours >= 24:
            self.hours = 0
本文链接:https://www.f2er.com/3121511.html

大家都在问