时间没有定义 - 类python

时间:2014-01-16 12:10:18

标签: python class nameerror

我一直试图让这个名为Time的类具有小时,分钟和秒的属性,它还具有存取函数和mutator函数,如set_hour,increment_hour等。

这是我的代码,我无法让它工作我得到错误时间未定义或当我切换最后一行时未定义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 个答案:

答案 0 :(得分:1)

set_hour("1")初始化该变量之前,您似乎在变量t上调用方法t = Time()

编辑:并按照评论中的说明更正缩进。我不是Python程序员所以我没有抓住它。