Python时钟类

时间:2016-11-27 02:40:54

标签: python class

class Clock:
    def __init__(self, hrsIn, minsIn, secsIn):    # to create the clock
       self.hours = hrsIn
       self.minutes = minsIn
       self.seconds = secsIn 

    def SetTime(self, newHrsIn, newMinsIn, newSecsIn):
      SetTime = self.hours
      SetTime = self.minutes
      SetTime = self.seconds

    def GetHours(self):
      return self.hours 

    def GetMinutes(self):
       return self.minutes

   def GetSeconds(self):
     return self.seconds

    def DisplayTime24(self):
      print("The time is",format(self.hours,"1d"),":",end=" ")
      if (self.minutes < 10):
        print("0",end="")
        print(format(self.minutes,"1d"),": ", end="")
      else:
        print(format(self.minutes, "1d"),": ", end="")
      if (self.seconds < 10):
        print("0",end="")
        print(format(self.seconds, "1d"),end="")
      else:
        print(format(self.seconds, "1d"),end="")
      print()


    def DisplayTime12(self):
      print("The time is ",end="")
      if (self.hours >= 12):
        t = self.hours - 12
        print(format(t,"2d"),": ",end="")
        if (self.minutes < 10):
            print("0",end="")
            print((self.minutes, "1d"),": ", end="")
        else:
            print(format(self.minutes, "1d"),": ", end="")
        if (self.seconds < 10):
            print("0",end="")
            print(format(self.seconds, "1d"),": ", end="")
        else:
            print(format(self.seconds, "1d"), end="")
        print(" PM")
      else:
        print(format(self.hours, "1d"), ": ",end="")
        if (self.minutes < 10):
           print("0",end="")
           print(format(self.minutes, "1d"),": ", end="")
        else:
            print(format(self.minutes, "1d"),": ", end="")
        if (self.seconds < 10):
            print("0",end="")
            print(format(self.seconds, "1d"),end="")
        else:
            print(format(self.seconds, "1d"),end="")
        print(" AM")

       # finish the rest of this class method(function) definition

   def IncrementClock(self):
     self.seconds = self.seconds + 1
     if (self.seconds == 60):
        self.seconds = 0
        self.minutes = self.minutes + 1
        if (self.minutes == 60):
            self.minutes = 0
            self.hours = self.hours + 1
        if (self.hours == 60):
            self.hours = 0
        else:
            print()
       # finish the rest of this class method(function) definition

  def main():
   myClock = Clock(0,0,0)     # create a clock with a time of midnight
   myClock.DisplayTime12()    # display it with a 12-hour format
   myClock.DisplayTime24()    # display it with a 24-hour format
   print()

   myClock.SetTime(22,30,5)  # change the time to 10:30:05 PM
   myClock.DisplayTime12()    # display it with a 12-hour format
   myClock.DisplayTime24()    # display it with a 24-hour format
   print()

   myClock.SetTime(23,59,59)  # change the time to 11:59:59
   myClock.DisplayTime12()    # display it with a 12-hour format
   myClock.DisplayTime24()    # display it with a 24-hour format
   print()
   myClock.IncrementClock()   # increment the clock 
   myClock.DisplayTime12()    # display new/current time in 12-hour format
   myClock.DisplayTime24()    # display new/current time in 24-hour format
   print()

 main() 

知道我可能会出错吗?它为所有人打印0。我不知道我的SetTime函数应该如何,但这就是它给我的东西。

The time is 0 : 00 : 00 AM
The time is 0 : 00 : 00

The time is 0 : 00 : 00 AM
The time is 0 : 00 : 00

The time is 0 : 00 : 00 AM
The time is 0 : 00 : 00

The time is 0 : 00 : 01 AM
The time is 0 : 00 : 01

这应该是它的样子。

 The time is 12: 00: 00 AM
 The time is 0: 00: 00

 The time is 10: 30: 05 PM
 The time is 22: 30: 05

 The time is 11: 59: 59 AM
 The time is 11: 59: 59

 The time is 12: 00: 00 PM
 The time is 12: 00: 00

1 个答案:

答案 0 :(得分:0)

只需更改SetTime即可将参数分配给成员变量,例如位于其上方的__init__

def SetTime(self, newHrsIn, newMinsIn, newSecsIn):
    self.hours = newHrsIn
    self.minutes = newMinsIn
    self.seconds = newSecsIn

您现在只需将成员变量分配给名为SetTime的变量,然后将其丢弃。