Question

I've been trying to make this class called Time with has the attributes of hour,minutes and seconds, that also has accessor functions and mutator functions such as set_hour, increment_hour and so on.

This is my code, I cannot get it to work I get the error Time is not defined or t is not defined when i switch the last lines around. Python 3.2.5 by the way.

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()
Was it helpful?

Solution

It seems you are invoking the method set_hour("1") on a variable t before that variable has been initialized by t = Time().

EDIT: and correct the indentation as said in the comments. I'm not a Python programmer so I didn't catch that.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top