2014-01-16 41 views
1

我一直在試圖讓這個類叫做Time,它具有小時,分鐘和秒的屬性,它還具有訪問器函數和mutator函數,比如set_hour,increment_hour等等。沒有定義時間 - python的類

這是我的代碼,我不能得到它的工作我得到的錯誤時間沒有定義或t沒有定義當我切換最後一行。順便說一下Python 3.2.5。

class Time: 
    """The Time class defines the time with 
    attributes: hour, minute, second 
    """ 
    #Attributes 
    hour = 12 
    minutes = 00 
    seconds = 00 

    #Functions 
    def get_hour(self): 
     return self.hour 

    def get_minute(self): 
     return self.minute 

    def get_second(self): 
     return self.second 

    def print_time(self): 
     print("Hello, the current time is", self.hour,":",self.minute,":",self.second) 

    def set_hour(self, new_hour): 
     self.hour = new_hour 

    t.set_hour("1") 
    print(t.get_hour()) 
    print(t.print_time()) 
    t = Time() 
+1

試着把't = Time()'放在第一位,並且不要這些行。 –

+1

在你的代碼中'類時間:'在文檔的頂部,並且你的代碼在底部不縮進? –

+0

對底部的線條進行不規則的修正,非常感謝! – Coder77

回答

1

似乎要調用一個變量t方法set_hour("1")之前變量已經被t = Time()初始化。

編輯:並按照評論中所述更正縮進。我不是一個Python程序員,所以我沒有理解。