6.1 Object-oriented class description defines a digital clock

Define a class description digital clock requirements: a given time, e.g. 15:50:00, the resulting effect is, as shown:

 

 

Import Time # Import time module 
class Clock (Object):
     DEF  the __init__ (Self, H, m, S):   # Time Initialization 
        self.hour = H
        self.minute = m
        self.second = s
    def run(self):  # 走字
        self.second += 1  # 秒+
        if self.second == 60:
            self.second = 0
            self.minute += 1
            if self.minute == 60:
                self.minute = 0
                self.hour += 1  
                if self.hour == 24:
                    self.hour = 0
     DEF Show (Self):   # show 
        Print ( " % 02d:% 02d:% 02d " % (self.hour, self.minute, self.second)) # before the number is less than two fill 0
 DEF clocktime ():
    H = int (INPUT ( " Enter h " ))
    m = int (INPUT ( " Enter min " ))
    S = int (INPUT ( " Enter seconds " ))
    clock = Clock(h,m,s)
    while True:
        clock.show()
        time.sleep(1)
        clock.run()
print(clocktime())

 Output:

 

03:09:42
03:09:43
03:09:44
03:09:45
03:09:46
03:09:47
03:09:48
03:09:49
03:09:50
03:09:51
03:09:52
03:09:53
03:09:54
03:09:55

Guess you like

Origin www.cnblogs.com/hrv5/p/12043972.html