2016-11-27 3 views
-2
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関数がどうあるべきか分かりませんが、これが私に与えるものです。Pythonクロッククラス

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 
+0

これを試してデバッグするために行ったことを示してください。変数の値を出力して、それらが期待どおりであることを確認しましたか? –

+0

SetTime関数でいくつか試してみましたが、そこに何があるのか​​分かりません。 –

答えて

0

ただ、ちょうどその上__init__のようにメンバ変数にパラメータを割り当てるためにSetTimeを変更する:あなたは今ちょうどその破棄されSetTimeと呼ばれる変数にメンバ変数を割り当てている

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

何。

+0

これは私が考えていたものです。非常に仲間ありがとう –