简体   繁体   English

Python中的简单时钟

[英]simple Clock in python

I wanted to program a simple clock in python: 我想用python编写一个简单的时钟:

class Clock:
    def __init__(self):
        self._hours = 0
        self._minutes = 0

    def gethours(self):
        return self._hours

    def getminutes(self):
        return self._minutes

    def sethours(self, hours):
        self._hours = hours

    def setminutes(self,minutes):
        if self._minutes > 60:
            self._hours = self._hours + 1
            self._minutes = 1
        else:
            self._minutes = minutes

    def tic(self):
        if self._minutes > 58:
            self._hours = self._hours + 1
            self._minutes = 1
        else:
            self._minutes = self._minutes + 1

    minutes = property(getminutes, setminutes)
    hours = property(gethours, sethours)

But when I now wanted to check it with the following code I get an AssertionError at assert(c.hours == i and c.minutes == j) 但是当我现在想用以下代码检查它时,我在assert(c.hours == i and c.minutes == j)处收到一个AssertionError。

c = Clock()

assert(c.minutes == 0 and c.hours == 0)

for i in range(24):
    assert(c.hours == i)
    for j in range(60):
        assert(c.hours == i and c.minutes == j)
        c.tic()

assert(c.minutes == 0 and c.hours == 0)

Here is a fixed rewriting, using the same test. 这是使用相同测试的固定重写。 The setters take care of setting correctly the values for hours and minutes (though hours' setter does not manage correctly adding more than one day, but this is easy to add based on the same model as for minutes). 设置器负责正确设置小时和分钟的值(尽管小时设置器无法正确添加超过一天的时间,但是基于与分钟相同的模型,这很容易添加)。

Then tic() is only a shortcut for adding one minute. 然后tic()只是增加一分钟的快捷方式。

Comment out the print statement to remove all hours:minutes. 注释掉print语句以删除所有小时:分钟。

Note: I've turned Clock to a "new style" class object, that allow to use property decorators and makes everything more readable (this not necessary for python3, but necessary to let the same code work also in python2). 注意:我将Clock变成了“新样式”类对象,该对象允许使用属性装饰器并使所有内容更具可读性(这对于python3而言不是必需的,但在python2中也可以使相同的代码起作用)。

class Clock(object):
    def __init__(self):
        self._hours = 0
        self._minutes = 0

    @property
    def hours(self):
        return self._hours

    @property
    def minutes(self):
        return self._minutes

    @minutes.setter
    def minutes(self, value):
        if value >= 60:
            # Take care, if one adds more than one hour, to
            # take the extra hours into account too:
            self.hours += value // 60
        self._minutes = value % 60

    @hours.setter
    def hours(self, value):
        self._hours = value % 24

    def tic(self):
        self.minutes += 1


c = Clock()

assert(c.minutes == 0 and c.hours == 0)

for i in range(24):
    assert(c.hours == i)
    for j in range(60):
        assert(c.hours == i and c.minutes == j)
        c.tic()
        print('{}:{}'.format(c.hours, c.minutes))

assert(c.minutes == 0 and c.hours == 0)

Excerpt and end of output: 摘录和输出结尾:

12:57
12:58
12:59
13:0
13:1
13:2
13:3
.
.
.
23:46
23:47
23:48
23:49
23:50
23:51
23:52
23:53
23:54
23:55
23:56
23:57
23:58
23:59
0:0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM